File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes blob.getBytes(1, (int) bl.length()); gives Nullpointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "blob.getBytes(1, (int) bl.length()); gives Nullpointer Exception" Watch "blob.getBytes(1, (int) bl.length()); gives Nullpointer Exception" New topic
Author

blob.getBytes(1, (int) bl.length()); gives Nullpointer Exception

tejz teja
Greenhorn

Joined: Feb 12, 2010
Posts: 7
Hi,

I stored file as a blob (datatype) in the DB.Saving data in the DB working fine over 1MB size.

But,when i am retrieving it from DB, it was showing Nullpointer Exception.

byte [] byt = bl.getBytes(1, (int) bl.length()); //here above 1MB size not working

I am using database as DB2.
if file size is less than 1MB , then it was working.

please let me know if i done any mistake in the code..


Thanks,
Teja


<%
try{
DBConnectionManager dbMgr = new DBConnectionManager();
Connection con=null;
con = dbMgr.getConnection();
ResultSet rs = null;
// byte[] byt=null;
String fileName="";
PreparedStatement psmnt = null;
// InputStream input1 =null;
OutputStream outStream = response.getOutputStream();
String fname=request.getParameter("fname");
GeneralUtil generalUtil = new GeneralUtil();
System.out.println("encMId----"+request.getParameter("encMId"));
String mid = generalUtil.decryptPassword(request.getParameter(" encMId"));
psmnt = con.prepareStatement("select * from agendadata2 where meetingid=? and filename=?");
psmnt.setString(1, mid);
psmnt.setString(2, fname.trim());
rs = psmnt.executeQuery();
if(rs.next()) {
System.out.println("hi i am in next..");
Blob bl = rs.getBlob("filedata");
System.out.println("bl.length()----"+bl.length());
long len = bl.length();
byte [] byt = bl.getBytes(1, (int) bl.length()); //here above 1MB size not working
System.out.println("byt----"+byt.length);
InputStream input1 = new ByteArrayInputStream(byt);
System.out.println("input1.available()----"+input1.available());
fileName=rs.getString("filename");
System.out.println("filename----"+fileName);
String fileType = fileName.substring(fileName.indexOf(".")+1,fileNam e.length());
if (fileType.trim().equalsIgnoreCase("txt")) {
response.setContentType( "text/plain" ); }
else if (fileType.trim().equalsIgnoreCase("doc"))
{ response.setContentType( "application/msword" );
}
else if (fileType.trim().equalsIgnoreCase("xls")) {
response.setContentType( "application/vnd.ms-excel" );
}
else if (fileType.trim().equalsIgnoreCase("pdf"))
{ response.setContentType( "application/pdf" );
System.out.println("i am in pdf----");
}
else
{ response.setContentType( "application/octet-stream" );
}
System.out.println("i am in after else----");
response.setHeader("Cache-Control", "max-age=60");
response.setContentLength(input1.available());
response.setHeader("Content-Disposition","attachment; filename="+ fileName);
response.setHeader("Cache-Control", "no-cache");
int sizeRead = 0;
System.out.println("i am in befor while----");
while ((sizeRead = input1.read(byt, 0, byt.length)) > 0)
{
outStream.write(byt, 0, sizeRead);
}
input1.close();
outStream.close();
}
} catch ( Exception Se ) {
Se.printStackTrace();
}
out.clear();
out = pageContext.pushBody();
%>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: blob.getBytes(1, (int) bl.length()); gives Nullpointer Exception