Search code examples
jsf-2primefacesjasper-reportsdownload

FileDownload Component of primefaces causes exception NullPointerException in report generation with JasperReports


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

<?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <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">

    <ui:composition template="/templates/interno.xhtml">
        <ui:define name="titulo">
            Módulo de Relatórios
        </ui:define>


        <ui:define name="corpo">

            <p:tabView id="tabView">
                <p:tab id="tab1" title="Download do relatorio das torres">
                    <div id="administracao_de_usuarios">

                        <h:form>
                            <span style="color: RGB(48, 122, 239);"> Para realizar o download do relatório, clique em uma das
                                opções de arquivo abaixo: </span>
                            <br />

                            <h:messages id="excecoes"
                                style="list-style-type: decimal; font-size: 14px; padding-left: 5px;"
                                title="Erros de preenchimento encontrados" />

                            <br />

                            <h:commandLink title="Relatório em PDF">
                                <f:setPropertyActionListener target="#{relatorioBean.tipoRelatorio}"
                                    value="1" />
                                <p:fileDownload value="#{relatorioBean.arquivoRetorno}" />
                                <h:graphicImage library="imagens" name="pdf.png" />
                                <span class="menu_link2">Arquivo PDF</span>
                            </h:commandLink>

                        </h:form>
                    </div>

                </p:tab>

            </p:tabView>


    </ui:define>
</ui:composition>
</html>

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?


Solution

  • After a long time without looking at stackoverflow, I realized that I needed to put the solution to this problem, which occurred a long time ago. So here it is:

    In this case I made the report template based on a database table structure, which had a nomenclature pattern.

    When I took the code to the server, there was a database table structure, which had a different naming pattern.

    In this way, the report was not generated and could not be downloaded, generating NullPointerException.

    After noticing that there were minimal changes to the database table structure of the server, I had to redo the report template with iReport Designer, based on the new table structure, which followed a different naming pattern than the previous table structure.

    So when I refactored the template based on the new structure, the report generation with JasperReports returned perfectly.