title: java.lang.NullPointerException while creating DiskFileItem
body: <p>I am trying to write a unit test for handling a file upload controller using Spring 3. Now if I send the image over to my service method through the controller everything works fine. But when doing a straight unit tests I am getting a null pointer exception. </p>
  
  <p>It appears that the property "dfos" inside the <code>DiskFilteItem</code> is null when I instantiate it manually but it is populated when retrieving a <code>MultipartFile</code> from the controller.</p>
  
  <pre><code>    File file = new File("//Users//test//Downloads//testimage.jpg");
      log.info("found file: " +file.exists());
      log.info("file size: " +file.length());
      String fieldName = "field";
      String contentType = "image/jpeg";
      boolean isFormField = false;
      String fileName = "testimage.jpg";
      int sizeThreshold = 10240;
  
      DiskFileItemFactory factory = new DiskFileItemFactory();
  
      DiskFileItemFactory factory = new DiskFileItemFactory();
      // throws null pointer
      FileItem fi = factory.createItem(fieldName,contentType,isFormField,fileName);
  
      // so does this one
      DiskFileItem item = new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, file);
      MultipartFile f = new CommonsMultipartFile(item);
  </code></pre>
  
  <p>I feel like I am missing something silly in my setup. My pom file contains the following dependencies.</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;commons-fileupload&lt;/groupId&gt;
          &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt;
          &lt;version&gt;1.2.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-io&lt;/groupId&gt;
          &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
          &lt;version&gt;2.0&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>This code throws the following stack trace</p>
  
  <pre>
  java.lang.NullPointerException
   at org.apache.commons.fileupload.disk.DiskFileItem.getSize(DiskFileItem.java:316)
   at org.springframework.web.multipart.commons.CommonsMultipartFile.(CommonsMultipartFile.java:60)
   at ImgurClientTest.testUploadImage(ImgurClientTest.java:58)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
   at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
   at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
   at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
   at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
   at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
   at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
   at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
   at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
   at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
   at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
  </pre>