title: FileDownload Component of primefaces causes exception NullPointerException in report generation with JasperReports
body: <p>I have an application for reporting. When I deploy and run locally, it generates the report and it downloads perfectly, no problems. (Creates the file in the application reports and file download is done)</p>
  
  <p>However, when I deployed the application on the company server, I tried to print the report and the exception occurred NullPointterException.</p>
  
  <p>Here's the stack trace:</p>
  
  <pre><code>Aug 21, 2013 5:26:01 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [FacesServlet] in context with path [/WebMap] threw exception [javax.servlet.ServletException] with root cause
  java.lang.NullPointerException
      at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:53)
      at javax.faces.event.ActionEvent.processListener(ActionEvent.java:84)
      at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:773)
      at javax.faces.component.UICommand.broadcast(UICommand.java:296)
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:310)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at web.web.filter.ConexaoHibernateFilter.doFilter(ConexaoHibernateFilter.java:31)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:344)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:110)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:98)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:95)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:79)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:120)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:55)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:36)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:106)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:80)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
      at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:150)
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>The code portion of this application is the one below:</p>
  
  <p>Download Page</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
      &lt;html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:p="http://primefaces.prime.com.tr/ui"&gt;
  
      &lt;ui:composition template="/templates/interno.xhtml"&gt;
          &lt;ui:define name="titulo"&gt;
              Módulo de Relatórios
          &lt;/ui:define&gt;
  
  
          &lt;ui:define name="corpo"&gt;
  
              &lt;p:tabView id="tabView"&gt;
                  &lt;p:tab id="tab1" title="Download do relatorio das torres"&gt;
                      &lt;div id="administracao_de_usuarios"&gt;
  
                          &lt;h:form&gt;
                              &lt;span style="color: RGB(48, 122, 239);"&gt; Para realizar o download do relatório, clique em uma das
                                  opções de arquivo abaixo: &lt;/span&gt;
                              &lt;br /&gt;
  
                              &lt;h:messages id="excecoes"
                                  style="list-style-type: decimal; font-size: 14px; padding-left: 5px;"
                                  title="Erros de preenchimento encontrados" /&gt;
  
                              &lt;br /&gt;
  
                              &lt;h:commandLink title="Relatório em PDF"&gt;
                                  &lt;f:setPropertyActionListener target="#{relatorioBean.tipoRelatorio}"
                                      value="1" /&gt;
                                  &lt;p:fileDownload value="#{relatorioBean.arquivoRetorno}" /&gt;
                                  &lt;h:graphicImage library="imagens" name="pdf.png" /&gt;
                                  &lt;span class="menu_link2"&gt;Arquivo PDF&lt;/span&gt;
                              &lt;/h:commandLink&gt;
  
                          &lt;/h:form&gt;
                      &lt;/div&gt;
  
                  &lt;/p:tab&gt;
  
              &lt;/p:tabView&gt;
  
  
      &lt;/ui:define&gt;
  &lt;/ui:composition&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>RelatorioBean</p>
  
  <pre><code>import java.util.HashMap;
  
  import javax.faces.application.FacesMessage;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.RequestScoped;
  import javax.faces.context.FacesContext;
  
  import org.primefaces.model.StreamedContent;
  
  import web.util.RelatorioUtil;
  import web.util.UtilException;
  
  @ManagedBean(name = "relatorioBean")
  @RequestScoped
  public class RelatorioBean {
  
      private StreamedContent arquivoRetorno;
      private int tipoRelatorio;
  
      public StreamedContent getArquivoRetorno() {
          FacesContext context = FacesContext.getCurrentInstance();
          String nomeRelatorioJasper = "relatorio";
          String nomeRelatorioSaida = "Relatório_torres_de_transmissão";      
          RelatorioUtil relatorioUtil = new RelatorioUtil();
          HashMap parametrosRelatorio = null;
  
          try {
              this.arquivoRetorno = relatorioUtil.geraRelatorio(
                      parametrosRelatorio, nomeRelatorioJasper,
                      nomeRelatorioSaida, this.tipoRelatorio);
          } catch (UtilException e) {
              context.addMessage(null, new FacesMessage(e.getMessage()));
              return null;
          }
          return this.arquivoRetorno;
      }
  
      public void setArquivoRetorno(StreamedContent arquivoRetorno) {
          this.arquivoRetorno = arquivoRetorno;
      }
  
      public int getTipoRelatorio() {
          return tipoRelatorio;
      }
  
      public void setTipoRelatorio(int tipoRelatorio) {
          this.tipoRelatorio = tipoRelatorio;
      }
  
  }
  </code></pre>
  
  <p>RelatorioUtil</p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.InputStream;
  import java.sql.Connection;
  import java.sql.SQLException;
  import java.util.HashMap;
  
  import javax.faces.context.FacesContext;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import net.sf.jasperreports.engine.JRException;
  import net.sf.jasperreports.engine.JRExporter;
  import net.sf.jasperreports.engine.JRExporterParameter;
  import net.sf.jasperreports.engine.JasperFillManager;
  import net.sf.jasperreports.engine.JasperPrint;
  import net.sf.jasperreports.engine.JasperReport;
  import net.sf.jasperreports.engine.export.JRPdfExporter;
  
  import net.sf.jasperreports.engine.util.JRLoader;
  
  import org.primefaces.model.DefaultStreamedContent;
  import org.primefaces.model.StreamedContent;
  
  import web.util.UtilException;
  
  public class RelatorioUtil {
  
      public static final int RELATORIO_PDF = 1;
  
  
      public StreamedContent geraRelatorio(HashMap parametrosRelatorio,
              String nomeRelatorioJasper, String nomeRelatorioSaida,
              int tipoRelatorio) throws UtilException {
          StreamedContent arquivoRetorno = null;
  
          try {
              FacesContext context = FacesContext.getCurrentInstance();
              Connection conexao = this.getConexao();
              String caminhoRelatorio = context.getExternalContext().getRealPath(
                      "relatorios");
  
  
              String caminhoArquivoJasper = caminhoRelatorio + File.separator
                      + "relatorioTorres" + ".jasper";
  
              String caminhoArquivoRelatorio = null;
  
              JasperReport relatorioJasper = (JasperReport) JRLoader
                      .loadObject(caminhoArquivoJasper);
              JasperPrint impressoraJasper = JasperFillManager.fillReport(
                      relatorioJasper, parametrosRelatorio, conexao);
              JRExporter tipoArquivoExportado = null;
              String extensaoArquivoExportado = "";
              File arquivoGerado = null;
  
              switch (tipoRelatorio) {
              case RelatorioUtil.RELATORIO_PDF:
                  tipoArquivoExportado = new JRPdfExporter();
                  extensaoArquivoExportado = "pdf";
                  break;
              default:
                  tipoArquivoExportado = new JRPdfExporter();
                  extensaoArquivoExportado = "pdf";
                  break;
              }
              caminhoArquivoRelatorio = caminhoRelatorio + File.separator
                      + nomeRelatorioSaida + "." + extensaoArquivoExportado;
              arquivoGerado = new java.io.File(caminhoArquivoRelatorio);
              tipoArquivoExportado.setParameter(JRExporterParameter.JASPER_PRINT,
                      impressoraJasper);
              tipoArquivoExportado.setParameter(JRExporterParameter.OUTPUT_FILE,
                      arquivoGerado);
              tipoArquivoExportado.exportReport();
              arquivoGerado.deleteOnExit();
  
              InputStream conteudoRelatorio = new FileInputStream(arquivoGerado);
              arquivoRetorno = new DefaultStreamedContent(conteudoRelatorio,
                      "application/" + extensaoArquivoExportado,
                      nomeRelatorioSaida + "." + extensaoArquivoExportado);
          } catch (JRException e) {
              throw new UtilException("Não foi possível gerar o relatório.", e);
          } catch (FileNotFoundException e) {
              throw new UtilException("Arquivo do relatório não encontrado.", e);
          }
          return arquivoRetorno;
      }
  
      private Connection getConexao() throws UtilException {
          java.sql.Connection conexao = null;
          try {
              Context initContext = new InitialContext();
              Context envContext = (Context) initContext
                      .lookup("java:/comp/env/");
              javax.sql.DataSource ds = (javax.sql.DataSource) envContext
                      .lookup("jdbc/WebConection");
              conexao = (java.sql.Connection) ds.getConnection();
          } catch (NamingException e) {
              throw new UtilException(
                      "Não foi possível encontrar o nome da conexão do banco.", e);
          } catch (SQLException e) {
              throw new UtilException("Ocorreu um erro de SQL.", e);
          }
          return conexao;
      }
  }
  </code></pre>
  
  <p>Can anyone help me to solve this error? Why this locally generating the file and performing the download, but this problem occurs on the server?</p>