Naveen Kumar Naveen Kumar - 4 months ago 13
jQuery Question

Error in uploading a file in spring and capturing response using ajax

I am uploading a file using Spring multipart file but getting a Null Pointer Exception when trying to capture its response using AJAX. I am able to successfully upload the file without capturing response through AJAX i.e, when I remove the id attribute of form or script is not executed due to some error.

JSP Form

<form action="actionXMLUpload" method="POST"
enctype="multipart/form-data" id="dealform">
Select XML file to upload <input type="file" name="file"
accept="text/xml" /> <input type="submit" value="Upload XML" />
</form>


AJAX Code-

$('#dealform').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data : $(this).serialize(), // get the form data
type : $(this).attr('method'), // GET or POST
url : $(this).attr('action'), // the file to call
success : function(response) { // on success..
$('#pages').html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting
});


Spring Controller-

@RequestMapping(value = "/actionXMLUpload", method = RequestMethod.POST)
public String uploadXML(FileUploadUtil xmlUtil, BindingResult result, Model model) {
if (result.hasErrors()) {
return "xmlupload";
}
try {
File destFile = new File("D:\\Travel_Portal\\"
+ xmlUtil.getFile().getOriginalFilename());
xmlUtil.getFile().transferTo(destFile);
dealXMLParser.xmlParser();
logger.info("********XML FILE SUCCESSFULLY UPLOADED*********");
model.addAttribute(MESSAGE, "XML File Successfully Uploaded!!!");
return "xmlupload";
} catch (Exception e) {
logger.error("********XML FILE NOT SUCCESSFULLY UPLOADED*********",
e);
model.addAttribute(MESSAGE, "Error Uploading XML File!!! Please Try Again!!!");
return "xmlupload";
}
}


Exception at File destFile = new File("D:\Travel_Portal\"
+ xmlUtil.getFile().getOriginalFilename());

java.lang.NullPointerException
at com.travelportal.controller.UploadController.uploadXML(UploadController.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

Answer

Using @RequestBody solved the problem for me. This was happening because file upload was taking some time and i needed the uploaded file content.