title: java.lang.NullPointerException at org.primefaces.component.filedownload.FileDownloadActionListener.processAction
body: <p>I am working on a JSF/PrimeFaces project where I am generating some files from a database and placing them in the web/resources folder of my project.I am using a p:datable to load the file names dynamically into a web page. The datatable has a singleMode activated. What I want is to make the files downloadable based on the users choice.when a file is selected and the download button is pushed an exception is thrown;  </p>
  
  <p>Here are the codes I used:</p>
  
  <h1>JSF page</h1>
  
  <pre><code>   &lt;div id="content"&gt;
          &lt;h:form id="resultForm"&gt;
              &lt;center&gt;
                  &lt;p:panel id="files" style="width: 80%"&gt;
                      &lt;p:dataTable id="resultDT" var="file" value="#{gen.outFileNames}" 
                                   selection="#{downloader.selectedFileName}" selectionMode="single" rowKey="#{file}"&gt;
                          &lt;f:facet name="header"&gt;
                              Generated Files
                          &lt;/f:facet&gt;
                          &lt;p:column headerText="File names"&gt;
                              &lt;h:outputText value="#{file}" /&gt;
                          &lt;/p:column&gt;
                          &lt;f:facet name="footer"&gt;
                              &lt;p:commandButton value="Download" ajax="false" icon="ui-icon-arrowthick-1-s"&gt;
                                  &lt;p:fileDownload value="#{downloader.file}"/&gt;
                              &lt;/p:commandButton&gt;
                          &lt;/f:facet&gt;
                      &lt;/p:dataTable&gt;
                  &lt;/p:panel&gt;
              &lt;/center&gt;
          &lt;/h:form&gt;
      &lt;/div&gt;
  </code></pre>
  
  <h1>Download Controller</h1>
  
  <pre><code>@ManagedBean(name = "downloader")
  @ViewScoped
  public class FilesDownloadController implements Serializable {
  
      private String selectedFileName;
  
      public StreamedContent getFile() {
          if (selectedFileName == null) {
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                      "Download warning", "Please click a file and click on the download button."));
              return null;
          }
          System.out.println("Download 1 : selectedFileName = " + selectedFileName);
          System.out.println("Download 2");
  
          System.out.println("Download 3");
          InputStream stream = this.getClass().getResourceAsStream(selectedFileName);
          System.out.println("Download 4");
          StreamedContent file = new DefaultStreamedContent(stream, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getMimeType(selectedFileName), 
          selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
          System.out.println("Download 5 = " + selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
          System.out.println(file != null ? "File content OK " + file.getContentType() + " " + file.getName() : "File content NOK");
          return file;
      }
  
      public String getSelectedFileName() {
          return selectedFileName;
      }
  
      public void setSelectedFileName(String selectedFileName) {
          this.selectedFileName = selectedFileName;
          System.out.println("Selected File changed: " + this.selectedFileName );
      }
  }
  </code></pre>
  
  <h1>Generator bean</h1>
  
  <pre><code>@ManagedBean(name = "gen")
  @SessionScoped
  public class GeneratorController implements Serializable {
  
  private List&lt;String&gt; outFileNames;
  
  public GeneratorController() {
      outFileNames = new ArrayList&lt;String&gt;();
      outFileNames.add("/resources/images/optimus.jpg");
      outFileNames.add("/resources/images/optimus_1.jpg");
      outFileNames.add("/resources/images/optimus_2.jpg");
      outFileNames.add("/resources/images/optimus_3.jpg");
  }
  
  public List&lt;String&gt; getOutFileNames() {
      return outFileNames;
  }
  
  public void setOutFileNames(List&lt;String&gt; outFileNames) {
      this.outFileNames = outFileNames;
  }
  }
  </code></pre>
  
  <h1>Exceptions thrown</h1>
  
  <pre><code>        Infos:   Selected File changed: /resources/images/optimus_1.jpg
          Infos:   Download 1 : selectedFileName = /resources/images/optimus_1.jpg
          Infos:   Download 2
          Infos:   Download 3
          Infos:   Download 4
          Infos:   Download 5 = optimus_1.jpg
          Infos:   File content OK image/jpeg optimus_1.jpg
          FATAL:   JSF1073 : javax.faces.FacesException intercepté durant le      traitement de INVOKE_APPLICATION 5 : UIComponent-ClientId=, Message=null
          FATAL:   No associated message
  javax.faces.FacesException
          at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
          at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
          at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
          at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
          at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
          at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
          at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
          at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
          at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
          at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
          at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
          at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
          at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
         at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
         at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
         at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
         at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
         at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
         at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
         at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
         at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
         at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
         at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
         at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
         at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
         at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.NullPointerException
         at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:81)
         at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
         at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
         at javax.faces.component.UICommand.broadcast(UICommand.java:300)
         at javax.faces.component.UIData.broadcast(UIData.java:1108)
         at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
         at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
         at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
      ... 31 more
  </code></pre>