Error generating report with Jasper Reports using JSF

Asked

Viewed 749 times

0

I’m following a tutorial which explains how to generate reports with iReport using JSF. I set up my report on iReport and it is working perfectly, I mean when I give a preview all data from the database I selected is loaded into the report. The problem starts when I try to generate that report:

That’s the first mistake:

java.lang.Nullpointerexception at java.lang.Class.isAssignableFrom(Native Method) at net.sf.jasperreports.engine.Fill.JRFillTextField.getFormat(Jrfilltextfield.java:706)

Then that:

GRAVE: 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 generating the 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 the generated report:

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

1 answer

0


Problem solved, 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, I preferred to have it open on the Jasper even that way: inserir a descrição da imagem aqui

My method was this way(I pass a code as parameter to return only the query referring to the object clicked in the table and also pass an image as parameter):

public void gerarRelatorio(Solicitacao rel) {
        Map<String, Object> filtro = new HashMap<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 mine commandButton which sits within a dataTable was like this:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.