0

i am trying to upload the image file onto the server. for that created a table and a field of BLOB. now i am trying to upload the image into database. but i am getting a null pointer exception. here is the servlet LITS_bin bin = new LITS_bin();

    bin.setProduct_name(request.getParameter("productName"));
    bin.setPrice(Integer.parseInt(request.getParameter("price")));
    String strpath = request.getParameter("txtFileName");
    System.out.println(strpath+bin.getProduct_name()+bin.getPrice());
    String filepath=strpath.substring(strpath.lastIndexOf("//")+1);
    System.out.println(filepath);
    File imgfile = new File(strpath);
    FileInputStream fin;
    try 
    {
        fin = new FileInputStream(imgfile);


        LITSdatabaseConnection.doConnection();
        String sql = "insert into product (product_name,price,image)values ?,?,?";


        PreparedStatement pst = LITSdatabaseConnection.getCon().prepareStatement(sql);
        pst.setString(1, bin.getProduct_name());
        pst.setFloat(2, bin.getPrice());
        pst.setBinaryStream(3, fin,imgfile.length());
        int i = pst.executeUpdate();
        System.out.println(i+"Record Inserted"+bin);
    }
    catch (FileNotFoundException fil) 
    {
        fil.printStackTrace();
        System.out.println(fil);            
    }
    catch (SQLException e) 
    {
        //System.out.println(e);
        e.printStackTrace();
    }
Nikhil
  • 9
  • 2
  • Why did you add the `javascript` tag? To me your whole approach seems to be wrong as you cannot access the file system of a user. Check this: http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – home Nov 27 '13 at 14:06
  • actually i want to add the image to database – Nikhil Nov 27 '13 at 14:07
  • Please carefully read the link I posted above... btw: in case you get NullPointerExceptions please **always** add the stacktrace to your question. – home Nov 27 '13 at 14:08
  • yes i am getting a null pointer exception and the root cause is that servlet is not getting the path of image file. – Nikhil Nov 27 '13 at 15:43

0 Answers0