title: Java getPart() throwing null pointer exception
body: <p>I am getting a null pointer exception when trying to use getPart() and I cannot understand why this is throwing an exception rather than just returning null.  Here is my form in jsp:</p>
  
  <pre><code>&lt;form onsubmit="return check()" method="post" enctype="multipart/form-data" action="/TrainingServlet"&gt;
     &lt;input id="hotel_file" type="file" name="hotel_file" size="30"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Then in my servlet, I have the following code:</p>
  
  <pre><code>public void doPost(HttpServletRequest req, HttpServletResponse resp) {
     try {
             Part filePart = req.getPart("hotel_file");  //this line throws null pointer exception
             getServletContext().getRequestDispatcher("/import.jsp").forward(req, resp);
  
     } catch(Exception e) {
  
             e.printStackTrace();
     }
  }
  </code></pre>
  
  <p>After reading this SO <a href="https://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet">post</a>, I added the @MultipartConfig annotation to the servlet class and I am still get NPE at the req.getPart() line.  Also I am running the servlet on JBoss 6.  Any insight appreciated!</p>
  
  <p>Here is the stacktrace</p>
  
  <pre><code>16:50:15,227 ERROR [STDERR] java.lang.NullPointerException
  16:50:15,228 ERROR [STDERR] at org.apache.catalina.connector.Request.getPart(Request.java:3225)
  16:50:15,228 ERROR [STDERR] at org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1102)
  16:50:15,229 ERROR [STDERR] at com.lanyon.training.javaproj.servlets.TrainingServlet.doPost(TrainingServlet.java:96)
  16:50:15,229 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
  16:50:15,230 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
  16:50:15,230 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324)
  16:50:15,231 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
  16:50:15,231 ERROR [STDERR] at com.lanyon.ghotel.common.pagination.web.PaginationFilter.doFilter(PaginationFilter.java:77)
  16:50:15,232 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
  16:50:15,232 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
  16:50:15,233 ERROR [STDERR] at com.lanyon.common.serverobjects.UTF8EncodingFilter.doFilter(UTF8EncodingFilter.java:33)
  16:50:15,233 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
  16:50:15,234 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
  16:50:15,235 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
  16:50:15,235 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
  16:50:15,236 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)
  16:50:15,236 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)
  16:50:15,236 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)
  16:50:15,237 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
  16:50:15,237 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
  16:50:15,238 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
  16:50:15,238 ERROR [STDERR] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
  16:50:15,239 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
  16:50:15,239 ERROR [STDERR] at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)
  16:50:15,240 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
  16:50:15,240 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
  16:50:15,241 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)
  16:50:15,241 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
  16:50:15,242 ERROR [STDERR] at java.lang.Thread.run(Thread.java:619)
  </code></pre>