We help IT Professionals succeed at work.
Get Started

Spring / Grails - file upload fails with NULL object

skithian asked
Last Modified: 2013-11-24
I am having a helluva time with multipart upload in Grails. Most sources point to that being very simple, and no additional configuration required. Yet that approach was giving me a null object on uploaded file.

Here is the exception i get:
ERROR errors.GrailsExceptionResolver  - Cannot get property 'originalFilename' on null object
java.lang.NullPointerException: Cannot get property 'originalFilename' on null object

I fiddled with adding Spring configuration to the resources.groovy:
beans = {

And attempted to do the same in the WEB-INF/applicationContext.xml:

   <bean id="multipartResolver"
            <property name="maxUploadSize" value="5000000"/>
   </bean >

Here the .gsp form code:
<g:form action="rawUpload" method="POST" name="uploadImageForm" enctype="multipart/form-data">
    <tr><td colspan="2" align="right"><div id="showImg" align="center">&nbsp; </div></td>
    <td colspan="2" align="right">
          <input type="file" name="Image" />
    <tr><td colspan="4">
            <div class="button" align="right">&nbsp;
                   <button type="submit" id="ImageBtn" name="ImageBtn" value="Upload Image" >Upload Image</button>

And in the controller:

//import org.springframework.web.multipart.MultipartHttpServletRequest;
//import org.springframework.web.multipart.commons.CommonsMultipartFile;

      def rawUpload = {
            //Spring MultipartHttpSevletRequest
            //MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
            //CommonsMultipartFile uploadedFile = (CommonsMultipartFile) mpr.getFile("reviewImage");
            //fileUploadCommand uploadedFile = new fileUploadCommand(params.reviewImg)
            log.debug "ImageController: request params are $params"
            def uploadedFile = request.getFile('reviewImg')
            if (!uploadedFile?.empty){
              //println "Class: ${uploadedFile.class}"
              //println "Name: ${uploadedFile.name}"
              //println "OriginalFileName: ${uploadedFile.originalFilename}"
              //println "Size: ${uploadedFile.size}"
              //println "ContentType: ${uploadedFile.contentType}"
              def webRootDir = servletContext.getRealPath("/")
              def reviewDir = new File(webRootDir, "/images_app/${session.user.login}")
              uploadedFile.transferTo( new File( reviewDir, uploadedFile.originalFilename))
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE