Results 1 to 4 of 4
  1. #1
    tejz is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Unhappy 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.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Please use the code tags to make the code more readible....does your database have a 1Mb limit on the size of the BLOB?

  3. #3
    tejz is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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....

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    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

  1. Nullpointer Exception???
    By kipcorn91 in forum AWT / Swing
    Replies: 5
    Last Post: 10-28-2010, 11:19 PM
  2. Reading from file - NullPointer exception
    By Sage25 in forum New To Java
    Replies: 5
    Last Post: 07-08-2010, 05:34 PM
  3. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 04:44 AM
  4. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 01:44 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 03:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •