java – Error generating report with Jasper Reports

Question:

I'm trying to generate a report with Jasper Reports + JSF and I'm getting the following exception:

net.sf.jasperreports.engine.JRException: Invalid page index range : 0 – -1 of 0 at net.sf.jasperreports.engine.print.JRPrinterAWT.printPages(JRPrinterAWT.java:136) at net.sf.jasperreports.engine .JasperPrintManager.print(JasperPrintManager.java:229) at net.sf.jasperreports.engine.JasperPrintManager.print(JasperPrintManager.java:116) at net.sf.jasperreports.engine.JasperPrintManager.printReport(JasperPrintManager.java:313) at bean.InputsBean.print(InputsBean.java:95)

This is my report code:

public void imprimir(){
        try{
            System.out.println("Imprimir chamado");
            String caminho = Faces.getRealPath("/reports/Insumos.jasper");

            Map<String, Object> parametros = new HashMap<>();

            Connection conexao = HibernateUtil.getConexao();

            JasperPrint relatorio =  JasperFillManager.fillReport(caminho, parametros, conexao);
            System.out.println(relatorio);
            JasperPrintManager.printReport(relatorio, true); //Linha do erro
        }catch(JRException erro){
            Messages.addGlobalError("Ocorreu um erro ao tentar gerar o relatório");
            erro.printStackTrace();
        }

    }//Fim do metodo imprimir

How can I solve this problem?

Answer:

Good evening! I managed to correct it.

In Jasper I had created 2 parameters and in my method I created the parameter variable, but I wasn't passing values ​​to it. For testing I created two puts with the parameters passing %% and then it ran without error. It looked like this:

public void imprimir(){
        try{
            System.out.println("Imprimir chamado");
            String caminho = Faces.getRealPath("/reports/Insumos.jasper");

            Map<String, Object> parametros = new HashMap<>();

            parametros.put("TIPO_INSUMO", "%%");
            parametros.put("NOME_INSUMO", "%%");

            Connection conexao = HibernateUtil.getConexao();

            JasperPrint relatorio =  JasperFillManager.fillReport(caminho, parametros, conexao);
            System.out.println(relatorio);
            JasperPrintManager.printReport(relatorio, true);
        }catch(JRException erro){
            Messages.addGlobalError("Ocorreu um erro ao tentar gerar o relatório");
            erro.printStackTrace();
        }

    }//Fim do metodo imprimir

Thanks!

Scroll to Top
AllEscort