• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
%>
 
reply
    Bookmark Topic Watch Topic
  • New Topic