2013-08-22 28 views
0

我對報告的應用程序。當我在本地進行部署和運行時,它會生成報告並完美下載,沒有問題。 (創建應用程序報告的文件和文件下載完成)FileDownload組件導致報表生成異常NullPointerException異常與JasperReports的

然而,當我部署在公司服務器上的應用程序,我試圖打印報告,發生NullPointterException例外。

這裏的堆棧跟蹤:

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) 

這個應用程序的代碼部分低於一個:

下載頁面

<?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; 
    } 

} 

Relato rioUtil

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; 
    } 
} 

任何人都可以幫助我解決這個錯誤嗎?爲什麼在本地生成文件並執行下載,但是在服務器上發生此問題?

回答

0

後很長一段時間不看計算器,我意識到我需要把解決這個問題,它發生在很久以前。所以在這裏,它是:

在這種情況下,我做了基於數據庫的表結構,它有一個命名模式的報告模板。

當我把代碼放到服務器上,有一個數據庫表結構,它有一個不同的命名模式。

這樣,未生成報告,無法下載,產生NullPointerException異常。

在注意到服務器的數據庫表結構發生了最小的更改後,我不得不使用iReport Designer重新生成報表模板,該模板基於新的表結構,該結構遵循與以前的表結構不同的命名模式。

因此,當我基於新結構重構模板時,JasperReports生成的報表完美地返回。

0

根據PrimeFaces 2.1 source codeFileDownloadActionListener 53行的NPE暗示#{relatorioBean.arquivoRetorno}實際上返回null。讓我們來看看有:

} catch (UtilException e) { 
    context.addMessage(null, new FacesMessage(e.getMessage())); 
    return null; 
} 

它看起來像它的返回nullUtilException是被扔。在文件下載請求中,添加faces信息是完全沒有意義的。它不會出現在任何地方。你最好的打印/日誌堆棧跟蹤

} catch (UtilException e) { 
    e.printStackTrace(); 
    return null; 
} 

,或者更好的,只是刪除整個try-catch和拋出該異常。

​​

這種方式,你將面臨的真正例外代替NullPointerException這是隱藏真正的問題。

+0

感謝您的展示方式。 我會對你展示的變化進行修改。 然後在這裏寫出結果。 –

相關問題