title: File upload using servlet returning NULL value as exception
body: <p>hi i am uploading images in to data base using <code>servlets</code>. when i submit my form i am getting a exception "NULL" here is my code any help are accepted.And can any one suggest me which way to be used to upload files in to database such that there wont be much of load on database and lessen <code>httprequest</code>. thank you in advance.</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1" import="requirementlogic.*,java.util.*"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;body&gt;
  &lt;form  method="post" action="../InsertImage"&gt;
  &lt;table&gt;
  &lt;tr&gt;&lt;TD &gt;&lt;B&gt;Upload Image&lt;/B&gt;&lt;/TD&gt;
  &lt;td&gt;&lt;input type="file" name="Image" size="20" value=""&gt;&lt;/TD&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;input type="submit" height="30" width="62"&gt; &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>
  
  <p><strong>here is my JDBC:</strong></p>
  
  <pre><code>package requirementlogic;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  
  public class DataBase {
      Connection con = null;
      String url = "jdbc:mysql://localhost:3306/";
      String dbname= "deshopa";
      String driver ="com.mysql.jdbc.Driver";
      String Username="root";
      String Password ="";
      public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
          Class.forName(driver).newInstance();
          con = DriverManager.getConnection(url+dbname,Username,Password);
          return con;
      }
  }
  </code></pre>
  
  <p><strong>here is my servlet:</strong></p>
  
  <pre><code>package requirementlogic;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.PreparedStatement;
  
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
   * Servlet implementation class InsertImage
   */
  @WebServlet("/InsertImage")
  public class InsertImage extends HttpServlet {
      private static final long serialVersionUID = 1L;
         DataBase db;
      /**
       * @see HttpServlet#HttpServlet()
       */
      public InsertImage() {
          super();
          // TODO Auto-generated constructor stub
      }
  
      /**
       * @see Servlet#init(ServletConfig)
       */
      public void init(ServletConfig config) throws ServletException {
          // TODO Auto-generated method stub
          super.init(config);
          db = new DataBase();
      }
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          response.setContentType("text/html;charset=UTF-8");
            PrintWriter pw = response.getWriter();
  
          try
          {
  
          String strpath=request.getParameter("image");
  
          String filepath=strpath.substring(strpath.lastIndexOf(".")+1);
          pw.println(filepath);
          File imgfile = new File(strpath);
          FileInputStream fin = new FileInputStream(imgfile);
          String sql = "insert into upload_image(image,image_name,image_length)";
          PreparedStatement pre = db.getConnection().prepareStatement(sql);
  
          pre.setBinaryStream(1,fin,(int)imgfile.length());
          pre.setString(2,filepath);
          pre.setLong(3,imgfile.length());
          pre.executeUpdate();
          pre.close();
          pw.println("done");
          }
          catch(Exception e){
              pw.println("direct exception");
          pw.println("Exception is "+ e.getMessage());
          }
      }
  
  }
  </code></pre>
  
  <p>Sql query used to create table:</p>
  
  <pre><code>CREATE TABLE `upload_image` (
  `Image_id` int(11) NOT NULL AUTO_INCREMENT ,
  `IMAGE` longblob NULL DEFAULT NULL ,
  `Image_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
  `image_length` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
  PRIMARY KEY (`Image_id`)
  )
  </code></pre>