File upload in Spring MVC gives NullPointerException
I have a simple JSP form like this:
<p>Please select a file and <i>click</i> <i>Upload file</i> to upload the file to the server:</p>
<c:url value="/upload/display" var="displayUploadedFileURL" />
<form:form action="${displayUploadedFileURL}" method="post" modelAttribute="upload" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Upload file" /> <form:errors path="file" />
<input type="Reset" value="Reset">
</form:form>
The file is uploaded to the server for the user. The controller method looks like this:
@Controller
@RequestMapping("/upload")
public class UploadController {
@Autowired
private UploadValidator uploadValidator;
@RequestMapping(value="/display", method=RequestMethod.POST)
public String displayUploadedFile(@ModelAttribute("upload") Upload upload,
BindingResult bindingResult,
Model model) {
// Validate Upload.
uploadValidator.validate(upload, bindingResult);
if (bindingResult.hasErrors()) {
return ("view/upload/select");
}
else {
String fileName = upload.getFile().getOriginalFilename();
System.out.println("Here: " + upload.getFile().getOriginalFilename());
model.addAttribute("fileName", fileName);
return ("view/upload/display");
}
}
...
But when I select a file and use the Upload File button, I get this:
Your page request has caused a NullPointerException: error:
library.validator.UploadValidator.validate(UploadValidator.java:29)
library.controller.upload.UploadController.displayUploadedFile(UploadController.java:45)
The corresponding validator is very simple:
package library.validator;
import library.model.Upload;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
@Component
public class UploadValidator implements Validator {
private static final Logger logger = Logger.getLogger(UploadValidator.class);
public UploadValidator() {
}
@Override
public boolean supports(Class cls) {
return Upload.class.isAssignableFrom(cls);
}
@Override
public void validate(Object target, Errors errors) {
logger.info(UploadValidator.class.getName() + ".validate() method called.");
Upload upload = (Upload) target;
if (upload.getFile().getSize() == 0) {
errors.rejectValue("file", "file.required");
}
}
}
I have all the relevant files .jar
in my application folder .lib
and I include the following:
<bean id="uploadValidator" class="library.validator.UploadValidator" />
<!-- Spring multipartResolver. -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
In DispatcherServlet
.
The object type Upload
looks like this:
import org.springframework.web.multipart.MultipartFile;
public class Upload {
private MultipartFile file;
And the corresponding getter and setter.
So why is my code not working?
Stacktrace for the current issue, i.e. for making changes to the controller method in the answer below:
org.springframework.util.Assert.notNull (Assert.java:112) org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName (RequestParamMethodArgumentResolver.JavaMed. AbstractNamedValueMethodArgumentResolver.java:89) org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument (HandlerMethodArgumentResolverComposite.java:79) org.springframework.web. springframework.web.method.support.InvocableHandlerMethod.invokeForRequest (InvocableHandlerMethod.java:124) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeHandHandletspringframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod (RequestMappingHandlerAdapter.java:749) org.springframework.web.servlet.mvc.method.annotation.RequestMappingMappingHandlerHandapter.is. web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle (AbstractHandlerMethodAdapter.java:83) org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.java:945) org.springframework.web:945 java: 876) org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java:961) org.springframework.web.servlet.FrameworkServlet.doPost (FrameworkServlet.java:863) javax.Hservlet.http. HttpServlet.java:641) org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java:837) javax.servlet.http.HttpServlet.service (HttpServlet.java:722) org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:304) orgina.apache ApplicationFilterChain.doFilter (ApplicationFilterChain.java:210) org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java:240) org.apache.catalina.core.StandardContextValve.invoke (StandardConteava.voke) catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:498) org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java:164) org.apache.catalina.valves.Error:ReportValve.invava100) org.apache.catalina.valves.AccessLogValve.invoke (AccessLogValve.java:562) org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java:118) org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:394) org.apache.coyote.http11.Http11Processor.process (Http11Processor.java:243) org.apache.coyote.http11.Http11ProtocolHttp11.Processor java: 188) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process (Http11Protocol.java:166) org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run (JIoEndpoint java.util:302) concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:617) java.lang.Thread.run (Thread.java:744)188) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process (Http11Protocol.java:166) org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run (JIoEndpoint.juava:302) java.curtil. ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:617) java.lang.Thread.run (Thread.java:744)188) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process (Http11Protocol.java:166) org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run (JIoEndpoint.juava:302) java.curtil. ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:617) java.lang.Thread.run (Thread.java:744)
source to share