Problem using Jasper Reports in vaadin

Hi i am trying to use Jasper Reports in Vaadin and am getting the following error:

SEVERE: log4j:WARN No appenders could be found for logger (net.sf.jasperreports.extensions.ExtensionsEnvironment).
SEVERE: log4j:WARN Please initialize the log4j system properly.
SEVERE: java.lang.NullPointerException
at java.io.ByteArrayInputStream.(ByteArrayInputStream.java:89)
at com.example.vaadin.DemoWindow$1.getStream(DemoWindow.java:148)

My code is as follows:

public void addFiletoWindow(Window rightWindow) {

    try {
        
        report = (JasperReport) JRLoader.loadObject(getClass().getResourceAsStream("report1.jasper"));

// System.out.println(“we’re in”);
paramMap = new HashMap();

    } 
    catch (JRException ex) {

        ex.printStackTrace();
    }

    StreamResource.StreamSource source = new StreamResource.StreamSource() {

        @Override
        public InputStream getStream() {
            byte[] b = null;
            try {

                 String queryString = "SELECT * From profile";
                // System.out.println(queryString);
                Statement stmt = DatabaseHelper.connection.createStatement();
                ResultSet rset = stmt.executeQuery(queryString);
                JRResultSetDataSource jrds = new JRResultSetDataSource(rset);

// while (rset.next()) {
// System.out.println(rset.getString(“user_id”));
// }

                b = JasperRunManager.runReportToPdf(report, paramMap, jrds);                   

            } 
            catch (JRException ex) {
             //   System.out.println(ex);
                System.out.println("JRException");
            } 
            catch (SQLException ex) {
                // System.out.println(ex);
                System.out.println("SQLException");
            }
            return new ByteArrayInputStream(b);
        }
    };

    StreamResource resource = new StreamResource(source,
            "myreport.pdf", MyApplication.getInstance());
    resource.setMIMEType("application/pdf");
    rightWindow.open(resource);

}

I have that my jasper file is placed at correct location and also the query is being retrieved from the database so my resultset is as required

Someone please help
Thanks in advance

reportToPdf is very likely returning null; do your variables really contain what you think prior to calling it ?

thanks for the prompt reply…
i have checked that my sql query returns the required result…
How can I check whether the rest of my variables are being loaded with the required value

This has most likely nothing at all to do with Vaadin.¸

Is b null as I think it is ?
Is getClass().getResourceAsStream(“report1.jasper”) indeed the stream you think it is ?
and so on.

I too initially thought that this problem had nothing to do with Vaadin but the same code worked as desired when I used it in a simple Java Desktop Application.

Class loaders do not quite work the same in a web application as they do in a straight Java desktop app, there is also the tricky fact that the files you run are actually copies of the files in your source directory; are you sure that you are loading your .jasper file ? Where is it being copied in the WAR / in your deployment directories ? Etc.

thanks a lot :slight_smile:
it worked

instead of using
report = (JasperReport) JRLoader.loadObject(getClass().getResourceAsStream(“report1.jasper”));
i used
report = (JasperReport) JRLoader.loadObject((“report1.jasper”));
and placed the report1.jasper file in the folder in “C:\Program Files\glassfish-3.1\glassfish\domains\domain1”

another question will this affect when i try to create an installer for my project as the file is nowhere in my project folder and is placed separately on the server

For installing a web application, there are two standard formats, namely “war” and “ear”. For a simple application, “war” is sufficient. Installing a web application is normally a two-step process: tell the container (glassfish in your case) to read the war and deploy it. The second step is to define the databases to the web container (normally, one does NOT define the JDBC connections directly in the code – the web server provides production-level facilities to share database connections). You should read about using JNDI to define your database.

So writing an installer for a web application is not normally needed: the files are self-packaged in the WAR, and the only intervention for the user is telling the server where the database is, using the web server’s JNDI configuration screens or configuration files.