Error generating report with Jasper Reports

Asked

Viewed 573 times

3

I’m trying to generate a report with Jasper Reports + JSF and I’m having 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.InsumesBean.print(Insumosbean.java:95)

That’s 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?

  • Whoa, Crislayne, did you check the data source that feeds your report? Do you feed it via Query or would it be by Collection? If it is the case of Collection you are not passing in the code snippet above. Check these details for us!

  • Hello, Julio Cesar! So it is by query that I feed with Hibernate connection. When Gero it in Jasper Reports, it generates normal with the database data in preview.

1 answer

1

Good afternoon! I managed to correct.

In Jasper I had created 2 parameters and in my method I created the variable parameters, but I was not passing values to it. For testing I created two puts with the parameters passing %% and then it ran without error. It was 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

Thank you!

Browser other questions tagged

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