0

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:

<form onsubmit="return check()" method="post" enctype="multipart/form-data" action="/TrainingServlet">
   <input id="hotel_file" type="file" name="hotel_file" size="30">
</form>

Then in my servlet, I have the following 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();
   }
}

After reading this SO post, 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!

Here is the stacktrace

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)
Community
  • 1
  • 1
homegrown
  • 187
  • 1
  • 1
  • 14
  • @PM77-1 stack trace posted – homegrown Nov 21 '14 at 22:58
  • Is line # **96** in `TrainingServlet.java` file the line that you commented? – PM 77-1 Nov 21 '14 at 23:06
  • Please post line # **77** and the surrounding code from `PaginationFilter.java` file. It seems to be the only other entry from the log that point to **your** code. – PM 77-1 Nov 21 '14 at 23:12
  • What `return check()` is doing may be relevant here. If you are stopping the normal HTML submit and swapping in an Ajax submit, that's the problem (i.e. its losings its multipartedness). – developerwjk Nov 21 '14 at 23:17
  • The point of you are using servlet 3.0? Those annotations on servlet class are useless if you are using servlet api version lower that required. – Roman C Nov 22 '14 at 12:57

1 Answers1

0

multipart gives you a byte stream. you have to convert it into a file.

you can also use transferTo() method of multipart

prsutar
  • 429
  • 2
  • 4
  • 17