Results 1 to 4 of 4
- 02-08-2011, 02:35 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
blob.getBytes(1, (int) bl.length()); gives Nullpointer Exception
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();
%>
Last edited by tejz; 02-09-2011 at 01:07 AM.
- 02-08-2011, 03:09 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Please use the code tags to make the code more readible....does your database have a 1Mb limit on the size of the BLOB?
- 02-08-2011, 03:34 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
No.i can able to store upto 20MB blob size in the DB.
When retrieving from the DB gives Null Pointer Exception for More than 1MB.
lessthan 1MB working fine.
plz help me....
sorry for bad code tags...
Thanks for ur reply....
- 02-10-2011, 06:37 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Since you're planning on streaming this straight out anyway why not simply use getBinaryStream()? That is essentially all you're doing. Skip the Blob entirely.
Also when asking a question on an exception you need to post the full stack trace. If that is the NPE line then bl is null which, from that code, is clearly not possible.
Similar Threads
-
Nullpointer Exception???
By kipcorn91 in forum AWT / SwingReplies: 5Last Post: 10-28-2010, 11:19 PM -
Reading from file - NullPointer exception
By Sage25 in forum New To JavaReplies: 5Last Post: 07-08-2010, 05:34 PM -
NullPointer exception
By bdario1 in forum New To JavaReplies: 15Last Post: 03-17-2010, 04:44 AM -
nullpointer exception in jsp
By fiero in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 11-07-2008, 01:44 PM -
NullPointer Exception
By Preethi in forum New To JavaReplies: 8Last Post: 02-06-2008, 03:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks