title: UploadedFile - NullPointerException
body: <p>I am trying to fileupload component of Primefaces 3.0 based on the example given in the demo. I have also added commons-fileupload-1.2.2.jar and commons-io-2.0.1.jar along with primefaces-3.1.jar file.</p>
  
  <p>I run the uploader I get a java.lang.NullPointerException could anyone please help me with this problem, I have searched the internet extensively and cannot find a solution that works.</p>
  
  <p>I am using NetBeans 7.2.1 and GlassFish to deploy.</p>
  
  <p>My Codes : 
  XHTML : </p>
  
  <pre><code>&lt;h:form id="frm" enctype="multipart/form-data"&gt;  
          &lt;table&gt;
              &lt;tr&gt;&lt;td&gt;&lt;div style="height:300px;"&gt;&lt;img src="images/adcar.jpg" style="top:0px;"/&gt;
                      &lt;/div&gt;
                      &lt;/td&gt;
                  &lt;td&gt;
                      &lt;h:form id="img" enctype="multipart/form-data"&gt;
   &lt;div style=" -moz-border-radius:15px;  border:1px solid;  width:600px; border-color:#930303;"&gt;
      &lt;br&gt;&lt;/br&gt;
          &lt;p:panel style="border:none;"&gt;
           &lt;p:messages&gt;&lt;/p:messages&gt;
          &lt;/p:panel&gt;
          &lt;h:panelGrid columns="2"&gt;
             &lt;h:outputText value="Nom Projet :"/&gt;
             &lt;h:inputText id="Nom" value="#{ajoutprjt.nomProjet}" /&gt;
             &lt;h:outputText value="Ref Projet :"&gt;&lt;/h:outputText&gt;&lt;h:inputText value="#{ajoutprjt.idProjet}" /&gt;
             &lt;h:outputText value="Date de Début :"&gt;&lt;/h:outputText&gt;&lt;h:inputText value="#{ajoutprjt.dateDeb}" /&gt;
             &lt;h:outputText value="Date de Fin :"&gt;&lt;/h:outputText&gt;&lt;h:inputText value="#{ajoutprjt.dateFin}" /&gt;
             &lt;h:outputText value="Chef  :"&gt;&lt;/h:outputText&gt;
             &lt;h:selectOneMenu value="#{ajoutprjt.idChef}"&gt;
                 &lt;f:selectItem itemValue="#{null}" itemLabel="-- select one --" /&gt;
                 &lt;f:selectItems value="#{ajoutprjt.tdata}" var="cmd" 
                                               itemValue="#{cmd.id}" itemLabel="#{cmd.id} #{cmd.nom} #{cmd.prenom}" /&gt;
             &lt;/h:selectOneMenu&gt;
             Documents : &lt;p:fileUpload value="#{ajoutprjt.file}" id="fileupload"   mode="simple"&gt;&lt;/p:fileUpload&gt;
             &lt;h:outputText&gt;&lt;/h:outputText&gt;&lt;p:panel style="border: none;"&gt;&lt;p:commandButton value="Ajouter" action="#{ajoutprjt.ajoutP}" ajax="false"&gt;&lt;/p:commandButton&gt;&lt;p:commandButton value="Annuler" action="#{ajoutprjt.annuleraj}" ajax="false"&gt;&lt;/p:commandButton&gt;&lt;/p:panel&gt;
         &lt;/h:panelGrid&gt;
           &lt;/div&gt;
                      &lt;/h:form&gt;
       &lt;/td&gt;&lt;/tr&gt;  &lt;/table&gt;
          &lt;/h:form&gt;[/code]
  </code></pre>
  
  <p>my  Bean :</p>
  
  <pre><code>package beans;
  
  import com.persistence.Compte;
  import com.persistence.Projets;
  import com.persistence.geoUtil;
  import java.io.File;
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.logging.Logger;
  import javax.faces.application.FacesMessage;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.RequestScoped;
  import javax.faces.context.FacesContext;
  import javax.faces.model.DataModel;
  import javax.faces.model.ListDataModel;
  import org.hibernate.Query;
  import org.hibernate.Session;
  import org.hibernate.Transaction;
  import org.primefaces.model.UploadedFile;
  /**
   *
   * @author  SA
   */
  @ManagedBean
  @RequestScoped
  public class ajoutprjt implements Serializable{
  
       private String idpro;
       private String IdProjet;
       private String NomProjet;
       private String DateDeb;
       private String DateFin;
       private String nomimg;
       private UploadedFile file;  
       private String IdChef;
       private DataModel&lt;Compte&gt; tdata ;
  
         public ajoutprjt(){
  
                          }
        // getters et Setters 
  
      ...
  
      public UploadedFile getFile() {
          System.out.println("Je suis sous GetFile: " );
          return file;
      }
  
      public void setFile(UploadedFile file) {
          System.out.println("je Suis Sous SetFile : ");
          this.file = file;
      }
  
  
    public void ajoutP(){
      //  String p=Integer.toString(puis);
       System.out.println("Ajout Projet de ref : " +IdProjet);
       System.out.println("Ajout Projet de Idchef est  : " +IdChef);
       System.out.println("file a ajouter e  : " +this.getFile().getSize());
       if(IdProjet.equals("")==false &amp;&amp; NomProjet.equals("")==false  &amp;&amp; DateDeb.equals("")==false &amp;&amp; DateFin.equals("")==false &amp;&amp; IdChef.equals("")==false){
  
             try{
          Session se=geoUtil.getSessionFactory().getCurrentSession();
          Transaction tr=se.beginTransaction();
          Query q = se.createQuery("SELECT p from Projets p where IdProjet = :rf");
             q.setParameter("rf", IdProjet);     
             List&lt;Object[]&gt; list = (List&lt;Object[]&gt;) q.list();
             if(list.size() == 0){
  
           Projets p =new Projets();
  
           p.setNomProjet(NomProjet);
           p.setIdProjet(IdProjet);
           p.setDateDeb(DateDeb);
           p.setDateFin(DateFin);
           p.setIdChef(IdChef);
           // UploadedFile file = event.getFile();
            if(this.getFile()!= null) {  
                   String fileName = file.getFileName();
                   byte[] fileBytes = file.getContents();
                   p.setNomimg(fileName);
                   p.setImg(fileBytes);}
               else{
                        p.setNomimg("Pas de fichier");
                        String s = "a";
                        byte[] a = s.getBytes();      
                        p.setImg(a); 
                           }  
              se.persist(p);
              tr.commit();
  
           context.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,"Projet ajouté avec succes","Compte chef ajouté avec succes"));
       this.setNomProjet("");
           this.setIdProjet("");
           this.setDateDeb("");
           this.setDateFin("");
           this.setIdChef("");
  
             }else{
                 context.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,"Attention:Projet déjas existe","Attention"));
             }
             }
             catch(Exception e1){
                 e1.printStackTrace();
         context.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,"Attention:Erreur d'insertion","Attention"));
             }
            }
         else{
           context.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Attention Tous les champs doivent etre  remplis!!","Attention"));
         }
  
     }
  }
  </code></pre>
  
  <p>This is my web.xml file :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt;
          &lt;param-value&gt;Development&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;primefaces.THEME&lt;/param-name&gt;
          &lt;param-value&gt;aristo&lt;/param-value&gt;
      &lt;/context-param&gt;
      &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;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
          &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;faces/auth.xhtml&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
      &lt;resource-ref&gt;
          &lt;description&gt;Base de données pour app Ontologie
  Projet master Pro&lt;/description&gt;
          &lt;res-ref-name&gt;jdbc/gcn&lt;/res-ref-name&gt;
          &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
          &lt;res-auth&gt;Container&lt;/res-auth&gt;
          &lt;res-sharing-scope&gt;Shareable&lt;/res-sharing-scope&gt;
      &lt;/resource-ref&gt;
          &lt;filter&gt;
           &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
          &lt;filter-class&gt;
                  org.primefaces.webapp.filter.FileUploadFilter
           &lt;/filter-class&gt;
          &lt;/filter&gt;
          &lt;filter-mapping&gt;
              &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
               &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
                   &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
          &lt;/filter-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Any help would be greatly appreciated. 
  Here is the stack trace: </p>
  
  <pre><code>javax.faces.el.EvaluationException: java.lang.NullPointerException
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      at javax.faces.component.UICommand.broadcast(UICommand.java:315)
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
      at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
      at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
      at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
      at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
      at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
      at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: java.lang.NullPointerException
      at beans.ajoutprjt.ajoutP(ajoutprjt.java:176)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
      at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
      ... 32 more
  </code></pre>