Error generating Report in Jasper Ireports

0

I followed a blog on the web that teaches you how to generate reports, but I have 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 running 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

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

I do not know what it can be.

Thank you.

    
asked by anonymous 12.06.2015 / 14:09

1 answer

1

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

But I would start by looking at two things, first if your "inputStream" is not null, it might be that you did not find the report file and then you would look at the "connection" it might not have been able to connect to the database server of data.

    
14.10.2015 / 13:30