title: JSF 2.2 - File upload return java.lang.NullPointerException
body: <p><code>&lt;h:inputFile/&gt;</code> return a NullPointerException error. I know there is already a lot of stuff about this exception and uploading files with JSF but I've read it all without any success to diagnose this problem.</p>
  
  <p>I am using JSF 2.2 with GlassFish 4.1.1, without PrimeFaces or Tomahawk since you can handle the file upload with JSF 2.2. as I read. </p>
  
  <p>The goal is just to upload a picture on the remote filesystem, and copy the path to the DB to retrieve it later through a datatable.</p>
  
  <p>I've already checked :</p>
  
  <p>1 - My form has the <code>enctype="multipart/form-data"</code> attribute.</p>
  
  <p>2 - My form is not nested in another form though it is part of <code>&lt;ui:composition/&gt;</code>.</p>
  
  <p>3 - I have set all proper filter to process multipart/form-data type :</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;MyFacesExtensionsFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.apache.myfaces.webapp.filter.ExtensionsFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;MyFacesExtensionsFilter&lt;/filter-name&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>4 - When I remove the <code>&lt;h:inputFile/&gt;</code> tag and its part in the <code>create()</code> method, sending data works without any problem.</p>
  
  <p>5 - I tried to state the ManagedBean as <code>@ApplicationScoped</code>, <code>@ViewScoped</code> and <code>@RequestScoped</code> without any change.</p>
  
  <p>6 - I tried to put the initialize <code>Part</code> attribute in the ManagedBean with abstract methods without any success.</p>
  
  <hr>
  
  <p>Here is my form : </p>
  
  <pre><code>&lt;ui:composition xmlns="http://www.w3.org/1999/xhtml"
                  xmlns:f="http://xmlns.jcp.org/jsf/core"
                  xmlns:h="http://xmlns.jcp.org/jsf/html"
                  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                  xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
                  xmlns:t="http://myfaces.apache.org/tomahawk"&gt;
      &lt;h:form enctype="multipart/form-data"&gt;
          &lt;h:outputLabel value="Marque : " /&gt;
          &lt;h:inputText value="#{carController.car.brand}" /&gt;
          &lt;br/&gt;
          few same inputText
          &lt;h:outputLabel value="Photo : " /&gt;
          &lt;h:inputFile value="#{carController.picture}" required="true" requiredMessage="No file selected."/&gt;
          &lt;br/&gt;
          &lt;h:commandButton value="Valider" action="#{carController.create}" /&gt;
      &lt;/h:form&gt;
  &lt;/ui:composition&gt;
  </code></pre>
  
  <hr>
  
  <p>Here is my ManagedBean, <strong>I've let comments as it is other ways to process the image upload that I've tried</strong> :</p>
  
  <pre><code>@Named(value = "carController")
  @SessionScoped
  public class CarController implements Serializable {
      private static final long serialVersionUID = 1L;
      @EJB
      private CarFacade carFacade;
  
      private Car car;
      private Part picture;
  
      private Collection&lt;Car&gt; carCollection;
  
      public CarController() {car = new Car();}
  
      public String create() throws IOException {
  
      /*try (   InputStream inputStream = picture.getInputStream();
              FileOutputStream outputStream = new FileOutputStream("D:" +
              File.separator + "test" + File.separator +
              picture.getSubmittedFileName())) {
          int bytesRead = 0;
          final byte[] chunck = new byte[1024];
          while ((bytesRead = inputStream.read(chunck)) != -1) {
              outputStream.write(chunck, 0, bytesRead);
          }
      }*/
  
      /*Path folder = Paths.get("//resources//cars//");
      String filename = FilenameUtils.getName(picture.getName()); 
      String extension = FilenameUtils.getExtension(picture.getName());
      Path file = Files.createTempFile(folder, filename + "-", "." + extension);
      try (InputStream input = picture.getInputStream()) {
          Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
      }*/
      picture.write("\\resources\\cars\\"+picture.getSubmittedFileName()+"."+picture.getContentType());
      car.setPicturePath(picture.getSubmittedFileName());
      carFacade.create(car);
      return "/index.xhtml";
  }
  </code></pre>
  
  <hr>
  
  <p>Here is exception : </p>
  
  <pre><code>Caused by: java.lang.NullPointerException
      at Controller.CarController.create(CarController.java:57)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
      at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
      at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
      at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
      ... 41 more
  </code></pre>
  
  <hr>
  
  <p>Thank you for any idea that may help.</p>
  
  <p>EDIT :       </p>
  
  <p>THIS is responsible of the error : picture.write("\resources\cars\"+picture.getSubmittedFileName()+"."+picture.getC‌​ontentType());</p>
  
  <p>I've tried to add this to web.xml without any success :</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;multipart-config&gt;
      &lt;location&gt;\resources\cars\&lt;/location&gt;
      &lt;max-file-size&gt;1300000&lt;/max-file-size&gt;
      &lt;max-request-size&gt;20000000&lt;/max-request-size&gt;
      &lt;file-size-threshold&gt;50000&lt;/file-size-threshold&gt;
      &lt;/multipart-config&gt;
  &lt;/servlet&gt;
  </code></pre>