Error generating Report in Jasper Ireports

Asked

Viewed 490 times

0

I followed a blog on the web that teaches how to generate the reports, but I come across a situation that I can not solve.

In case I have the following codes:

Main java.

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    new Main().abrirRelatorioClientes();
}

public void abrirRelatorioClientes() {


    InputStream inputStream = getClass().getResourceAsStream( "/reportTeste.jasper" );


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






    try {


        ReportUtils.openReport( "reportTeste", inputStream, parametros,
                ConnectionFactory.getSakilaConnection() );

    } catch ( SQLException exc ) {
        exc.printStackTrace();
    } catch ( JRException exc ) {
        exc.printStackTrace();
    }

}

}

Connectionfactory.java.

public class ConnectionFactory {


static {
    try {

      Class.forName( "com.mysql.jdbc.Driver" );


    } catch ( ClassNotFoundException exc ) {


        exc.printStackTrace();

    }
}


public static Connection getConnection(
        String url,
        String usuario,
        String senha ) throws SQLException {


    return DriverManager.getConnection( url, usuario, senha );

}


public static Connection getSakilaConnection() throws SQLException {

    return getConnection(
            "jdbc:mysql://localhost/base",
            "root",
            "root" );

}

}

Reportutils.java

public class ReportUtils {

/**
 * Abre um relatório usando uma conexão como datasource.
 * 
 * @param titulo Título usado na janela do relatório.
 * @param inputStream InputStream que contém o relatório.
 * @param parametros Parâmetros utilizados pelo relatório.
 * @param conexao Conexão utilizada para a execução da query.
 * @throws JRException Caso ocorra algum problema na execução do relatório
 */
public static void openReport(
        String titulo,
        InputStream inputStream,
        Map<String, Object> parametros,
        Connection conexao ) throws JRException {


    JasperPrint print = JasperFillManager.fillReport(
            inputStream, parametros, conexao );


    viewReportFrame( titulo, print );

}

/**
 * Abre um relatório usando um datasource genérico.
 *
 * @param titulo Título usado na janela do relatório.
 * @param inputStream InputStream que contém o relatório.
 * @param parametros Parâmetros utilizados pelo relatório.
 * @param dataSource Datasource a ser utilizado pelo relatório.
 * @throws JRException Caso ocorra algum problema na execução do relatório
 */
public static void openReport(
        String titulo,
        InputStream inputStream,
        Map<String, Object> parametros,
        JRDataSource dataSource ) throws JRException {


    JasperPrint print = JasperFillManager.fillReport(
            inputStream, parametros, dataSource );


    viewReportFrame( titulo, print );

}


private static void viewReportFrame( String titulo, JasperPrint print ) {


    JRViewer viewer = new JRViewer( print );


    JFrame frameRelatorio = new JFrame( titulo );


    frameRelatorio.add( viewer, BorderLayout.CENTER );


    frameRelatorio.setSize( 500, 500 );


    frameRelatorio.setExtendedState( JFrame.MAXIMIZED_BOTH );


    frameRelatorio.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );


    frameRelatorio.setVisible( true );

}

}

When previewing the report in Ireports it appears the right data.

Only when executing it returns the following error:

Exception in thread "main" java.lang.NullPointerException
at java.lang.Class.isAssignableFrom(Native Method)
at net.sf.jasperreports.engine.fill.JRFillTextField.getFormat(JRFillTextField.java:706)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:394)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:368)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:258)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:499)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2033)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:760)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:270)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:378)
at tutorialrelatorios.util.ReportUtils.openReport(ReportUtils.java:45)
at tutorialrelatorios.Main.abrirRelatorioClientes(Main.java:50)
at tutorialrelatorios.Main.main(Main.java:27)
Java Result: 1

By the return something is getting null when running the report, in case I downloaded the example of the tutorial and used it in my test and ran smoothly, analyzed the two XML and are identical.

I don’t know what else it could be.

Thank you.

1 answer

1

Hello, the easiest way to find the problem is debugging.

But I would start looking at two things, first if your "inputStream" is not null, it may be that you have not found the report file and then would look at the "Connection" may be that you have failed to connect to the database server.

Browser other questions tagged

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