title: Spring MVC File Upload null multipart data
body: <p>I want to upload an image file to one location in my computer with Spring MVC. But when I chose the file I am getting NullPointerException error. Here is my codes;</p>
  
  <pre><code>@RequestMapping("/save-product")
  public void saveFile(HttpServletRequest servletRequest,
          @ModelAttribute("uploadedFile") UploadedFile uploadedFile,
          BindingResult bindingResult, Model model) {
  
      MultipartFile multipartFile = uploadedFile.getMultipartFile();
      String fileName = multipartFile.getOriginalFilename();
      try {
          File file = new File(servletRequest.getServletContext().getRealPath("/home/mesud/springupload/"), fileName);
          multipartFile.transferTo(file);
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  
  &lt;sf:form enctype="multipart/form-data" modelAttribute="uploadedFile"&gt;
                  &lt;fieldset &gt;
                   &lt;legend&gt;Resim Yükle&lt;/legend&gt;
                   &lt;div id='progressBar'
                      style='height: 20px; border: 2px solid green; margin-bottom: 20px'&gt;
                      &lt;div id='bar' style='height: 100%; background: #33dd33; width: 0%'&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;input type="file" id=profilResim value="Profil Resmi" style="margin-bottom: 20px"/&gt;&lt;br/&gt;
                &lt;/fieldset&gt;
  &lt;/sf:form&gt;
  </code></pre>
  
  <p>I am uploading file in onSelect function in Javascript. Here is my javascript method;</p>
  
  <pre><code>function uploadNext() {
          var xhr = new XMLHttpRequest();
          var fd = new FormData();
          var file = document.getElementById('profilResim').files[filesUploaded];
          fd.append("multipartFile", file);
          xhr.upload.addEventListener("progress", onUploadProgress, false);
          xhr.addEventListener("load", onUploadComplete, false);
          xhr.addEventListener("error", onUploadFailed, false);
          xhr.open("POST", "save-product");
          debug('uploading ' + file.name);
          xhr.send(fd);
      }
  </code></pre>
  
  <p>In my Applicaiton Initializer class I use this method for multipart requests;</p>
  
  <pre><code>@Override
  protected void customizeRegistration(Dynamic registration) {
      registration.addMapping("/");
      registration.setMultipartConfig(new   MultipartConfigElement("/home/mesud/springtemp",2097152, 4194304, 0));
  }
  </code></pre>
  
  <p>Why my UploadedFile object (this object is includes a MultipartFile object) is posted null?</p>