FileDownload Component of primefaces causes exception NullPointerException in report generation with JasperReports-Collection of common programming errors
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)
However, when I deployed the application on the company server, I tried to print the report and the exception occurred NullPointterException.
Here’s the stack trace:
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)
The code portion of this application is the one below:
Download Page
Módulo de Relatórios
Para realizar o download do relatório, clique em uma das
opções de arquivo abaixo:
Arquivo PDF
RelatorioBean
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;
}
}
RelatorioUtil
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;
}
}
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?
-
Based on PrimeFaces 2.1 source code, a NPE on line 53 of
FileDownloadActionListener
suggests that#{relatorioBean.arquivoRetorno}
actually returnednull
. Let’s look there:} catch (UtilException e) { context.addMessage(null, new FacesMessage(e.getMessage())); return null; }
It look like that it’s returning
null
when anUtilException
is been thrown. That adding of faces message is by the way completely pointless on a file download request. It won’t appear anywhere. You’d better print/log the stack trace} catch (UtilException e) { e.printStackTrace(); return null; }
or, better, just remove that whole try-catch and throw that exception directly.
public StreamedContent getArquivoRetorno() throws UtilException { // ... }
This way you’ll face the real exception instead of a
NullPointerException
which is hiding the real problem. -
Aug 26, 2013 3:27:49 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute WARNING: /restrito/relatorio.xhtml @51,66 value="#{relatorioBean.arquivoRetorno}": Error reading 'arquivoRetorno' on type webmap.web.RelatorioBean javax.el.ELException: /restrito/relatorio.xhtml @51,66 value="#{relatorioBean.arquivoRetorno}": Error reading 'arquivoRetorno' on type webmap.web.RelatorioBean at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111) at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:50) 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 webmap.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) Caused by: java.lang.Error: Unresolved compilation problem: Unhandled exception type UtilException at webmap.web.RelatorioBean.getArquivoRetorno(RelatorioBean.java:42) 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 javax.el.BeanELResolver.getValue(BeanELResolver.java:87) at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67) at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:71) at com.sun.el.parser.AstValue.getValue(AstValue.java:116) at com.sun.el.parser.AstValue.getValue(AstValue.java:163) at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219) at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:106) ... 56 more
There is a new exception. This is some kind of permission error for writing the file?