Error generating report with Jasper Reports using JSF

0

I'm following a tutorial which explains how to generate reports with iReport using JSF . I mounted my report in iReport and it is working perfectly, that is when I give a preview all the data the bank I selected is loaded into the report. The problem starts when I try to generate this report:

This is the first error:

  

java.lang.NullPointerException         at java.lang.Class.isAssignableFrom (Native Method)       at net.sf.jasperreports.engine.fill.JRFillTextField.getFormat (JRFillTextField.java:706)

Then this:

  

SEVERE: System error: null javax.faces.FacesException at   com.sun.faces.lifecycle.InvokeApplicationPhase.execute (InvokeApplicationPhase.java:89)

And this:

  

Caused by: java.lang.NullPointerException at   org.primefaces.component.filedownload.FileDownloadActionListener.processAction (FileDownloadActionListener.java:81)

Method that generates report:

public StreamedContent getSampleReportPDF(){

    InputStream relatorio = null;

    try {
        String pdfFile = "C:\sampleReport.pdf";

        ByteArrayOutputStream Teste = new ByteArrayOutputStream();

        JasperReport jasperReport = (JasperReport)JRLoader.loadObject(getClass().getClassLoader().getResourceAsStream("Report/RelatorioReembolso.jasper"));
        jasperReport.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);

        HashMap<String, String> params = new HashMap<String, String>();
        JasperPrint print = JasperFillManager.fillReport(jasperReport, params, ConexaoMysql.abrir());

        JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();

        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, pdfFile);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, Teste);
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
        exporter.exportReport();

        relatorio = new ByteArrayInputStream(Teste.toByteArray());
    } catch (JRException ex) {
            ex.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new DefaultStreamedContent(relatorio);

}

Button that downloads generated report:

<p:commandButton rendered="true" id="exportar" title="Exportar" ajax="false">
    <p:fileDownload value="#{listarReembolsoBean.sampleReportPDF}" />
</p:commandButton>
    
asked by anonymous 21.08.2015 / 19:01

1 answer

0

Problem solved, I was using a very old version of Jasper I changed my dependency to this version:

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>5.0.1</version>
</dependency>

I made some changes to my method that generates the report too, so I preferred to have it open on the Jasper screen anyway:

Mymethodisthisway(IpassacodeasaparametertoreturnonlythequeryfortheobjectclickedonthetableandIalsopassanimageasaparameter):

publicvoidgerarRelatorio(Solicitacaorel){Map<String,Object>filtro=newHashMap<String,Object>();System.out.println("Codigo solicitacao:" + rel.getCodigo());
        try {
            InputStream image = this.getClass().getClassLoader().getResourceAsStream("Report/logo-unimed-correto.png");
            filtro.put("codigo", rel.getCodigo());
            filtro.put("image", image);

            JasperReport jasperReport = (JasperReport) JRLoader
                    .loadObject(getClass().getClassLoader().getResourceAsStream("Report/RelatorioReembolso.jasper"));
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, filtro, ConexaoMysql.abrir());
            JasperViewer jrviewer = new JasperViewer(jasperPrint, false);
            jrviewer.setVisible(true);
            jrviewer.toFront();
            FacesUtil.adicionarMsgInfo("Relatório Gerando com Sucesso!!");
        } catch (JRException ex) {
            ex.printStackTrace();
            FacesUtil.adicionarMsgErro("Erro ao gerar relatório: "+ex.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesUtil.adicionarMsgErro("Erro ao gerar relatório: "+ex.getMessage());
        }
    }

And my commandButton that stays inside a dataTable looks like this:

<p:commandButton icon="ui-icon-print"
        title="Exportar Relatório"
        actionListener="#{listarReembolsoBean.gerarRelatorio(reembolso)}"
        update=":frmPrin :msgGlobal">
</p:commandButton>
    
21.08.2015 / 22:45