title: File Upload in Spring 3 MVC - Null Pointer Exception
body: <p>I am using spring MVC 3.  I have been trying to access the attributes of the uploaded file but I keep getting the following error message.  I can access the other fields of the form that is posted but I can't access the uploaded file.</p>
  
  <pre><code>nullhandleForm - Failed to convert property value of type 'java.lang.String' to required type 
  'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'file'; 
  nested exception is java.lang.IllegalStateException: 
  Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile] 
  for property 'file': no matching editors or conversion strategy found 
  </code></pre>
  
  <p>HTML/JSP file</p>
  
  <pre><code>&lt;%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt;
  &lt;%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt;JSP Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;p&gt;${Response}&lt;/p&gt;   
      &lt;h1&gt;Upload Songs&lt;/h1&gt;        
      &lt;table&gt;
          &lt;form:form action="" commandName="handleForm"&gt;
          &lt;tr&gt;                
              &lt;td&gt;Song Name :&lt;/td&gt;
              &lt;td&gt;&lt;form:input path="songName"/&gt;&lt;/td&gt;                                        
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Artist Name :&lt;/td&gt;                        
              &lt;td&gt;&lt;form:input path="artistName"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Gendre :&lt;/td&gt;                        
              &lt;td&gt;&lt;form:input path="gendre"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;description :&lt;/td&gt;
              &lt;td&gt;&lt;form:textarea path="description"/&gt;&lt;/td&gt;
  
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Browse File :&lt;/td&gt;
              &lt;td&gt;&lt;form:input type="file" path="file" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td colspan="2" style="text-align: center"&gt;&lt;input type="submit" value="submit" /&gt;&lt;/td&gt;                        
          &lt;/tr&gt;            
          &lt;/form:form&gt;
      &lt;/table&gt;        
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>The form handlerclass</p>
  
  <pre><code>public class HandleForm {
  
  private String songName;
  private String artistName;
  private String gendre;
  private String description;    
  private CommonsMultipartFile file;
  
  
  public CommonsMultipartFile getFile() {
      return file;
  }
  
  public void setFile(CommonsMultipartFile file) {
      this.file            = file;     
  }
  
  public String getArtistName() {
      return artistName;
  }
  
  public void setArtistName(String artistName) {
      this.artistName = artistName;
  }
  
  public String getDescription() {
      return description;
  }
  
  public void setDescription(String description) {
      this.description = description;
  }
  
  public String getGendre() {
      return gendre;
  }
  
  public void setGendre(String gendre) {
      this.gendre = gendre;
  }
  
  public String getSongName() {
      return songName;
  }
  
  public void setSongName(String songName) {
      this.songName = songName;
  }  
  
  }
  </code></pre>
  
  <p>The controller</p>
  
  <pre><code>@Controller
  public class AdminController{   
  
  @RequestMapping(value = "/admin", method = RequestMethod.GET)
  public String showAdmin(){
      return "admin/index";
  }
  
  
  @RequestMapping(value = "/admin/upload-songs", method = RequestMethod.GET)
  public String showContacts(Model model) {
      model.addAttribute(new HandleForm());
      return "/admin/upload";
  }
  
  @RequestMapping(value = "/admin/upload-songs", method = RequestMethod.POST)
  public String doForm(@ModelAttribute(value = "handleForm") HandleForm handleForm, BindingResult result, Model model){
  
      if(result.hasErrors()){
          String stringList = null;
          List&lt;FieldError&gt; errors = result.getFieldErrors();
          for (FieldError error : errors ) {
              stringList += error.getObjectName() + " - " + error.getDefaultMessage() + "\n";
          }
          model.addAttribute("Response", stringList);
          //model.addAttribute("songname", handleForm.getSongName()); works fine
          //model.addAttribute("filename", handleForm.getFile().getOriginalFilename()); throws an error..?
      }       
  
      return "/admin/upload";
  }
  
  }
  </code></pre>
  
  <p>Any help would be much appreciated. Thanks in advance</p>