Search code examples
javajasper-reports

can't seem to view my ireport file in java


My problem is I can't seem to view the ireport file that i created and saved. here is the java coding:

private void btnreportActionPerformed(java.awt.event.ActionEvent evt) {                                          
    Connection c; 

    try {
        c = DBConnect.DB();
        String report = "C:\\Users\\p.bwalya\\Documents\\Reports\\NPS Report.jrprint ";
        JasperReport JR = JasperCompileManager.compileReport(report);
        JasperPrint JP = JasperFillManager.fillReport(JR,null,c);
        JasperViewer.viewReport(JP);
    } catch(SQLException | JRException e) {
        e.printStackTrace();
    }
}                                         

and here is the error message:

net.sf.jasperreports.engine.JRException:
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Invalid byte 1 of 1-byte UTF-8 sequence.
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:302)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:285)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:274)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:219)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:194)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:185)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:288)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:575)
at ireport.main(ireport.java:35)

Does anyone know what is wrong with my code?


Solution

  • You compile the jrxml file not the .jasper file (its already complied) nor the jrprint (JasperPrint object, its already filled)

    String report = "C:\\Users\\p.bwalya\\Documents\\Reports\\NPS Report.jrprint ";
    JasperReport JR = JasperCompileManager.compileReport(report);
    

    you need to pass the .jrxml

    es.

    String report = "C:\\Users\\p.bwalya\\Documents\\Reports\\NPS_Report.jrxml";
    JasperReport JR = JasperCompileManager.compileReport(report);
    

    If you like to know more about the different jasper report formats..

    what-is-the-difference-between-jasperreport-formats

    Note:

    1. if you already have the .jasper file there is no need to compile the jrxml, just pass the .jasper file to the fillReport instead of the JasperReport see @Lefteris Bab comment.
    2. in the fillReport consider passing an empty HashMap instead of null.