title: Part null pointer exception
body: <p>I am trying to upload a file but I get a NullPointerException along the way.</p>
  
  <p>The error I am getting is</p>
  
  <pre><code>SEVERE:   java.lang.NullPointerException at servlet.UploadServlet.doPost(UploadServlet.java:36)
  </code></pre>
  
  <p>on this line:</p>
  
  <pre><code>InputStream is = part.getInputStream();
  </code></pre>
  
  <p>Here is the code for servlet and jsp page.</p>
  
  <p><strong>UploadServlet</strong></p>
  
  <pre><code>    @MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10, maxFileSize = 1024 * 1024 * 50, maxRequestSize = 1024 * 1024 * 50)
  
  public class UploadServlet extends HttpServlet {
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          response.setContentType("text/html;charset=UTF-8");
          PrintWriter out = response.getWriter();
          try {
  
              Part part = request.getPart("file");
              InputStream is = part.getInputStream();
  
              String filename = getFileName(part);
              String relativeWebPath = "/WEB-INF/uploads";
              String absoluteFilePath = getServletContext().getRealPath(relativeWebPath);
              File uploadedFile = new File(absoluteFilePath, filename);
  
              FileOutputStream os = new FileOutputStream(uploadedFile);
  
              int i = is.read();
              while (i != -1) {
                  os.write(i);
                  i = is.read();
              }
              os.close();
          } catch (Exception ex) {
              ex.printStackTrace();
          } finally {
              out.close();
          }
      }
  
      private String getFileName(Part part) {
          for (String cd : part.getHeader("content-disposition").split(";")) {
              if (cd.trim().startsWith("filename")) {
                  String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
                  return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1);
              }
          }
          return null;
      }
  }
  </code></pre>
  
  <p><strong>upload.jsp</strong></p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;link rel="stylesheet" type="text/css" href="css/style.css"&gt;
          &lt;title&gt;MobMel::Upload&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form action="UploadServlet" method="post" enctype="multipart/form-data"&gt;
              &lt;table class="upload"&gt;
                  &lt;tr&gt;
                      &lt;td&gt;File&lt;/td&gt; 
                      &lt;td&gt;&lt;input type="file" name="file"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;/td&gt; 
                      &lt;td&gt;&lt;input type="submit" value="Upload"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;RegisterServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;servlet.RegisterServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;LoginServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;servlet.LoginServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;ProfileServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;servlet.ProfileServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;UploadServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;servlet.UploadServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;RegisterServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/RegisterServlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;LoginServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/LoginServlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;ProfileServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/ProfileServlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;UploadServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/UploadServlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>I hope you guys can help me fix this.</p>
  
  <p>Thanks</p>