I have created an application with report features. report features is created via iReport
My application also contains a reports folder containing all .japser files

Here is the code

public MyiReportViewer(InputStream stream,HashMap parameter)  
{  
this();  
try  
{  
/* load the required JDBC driver and create the connection 
here JDBC Type Four Driver for MySQL is used*/  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con =  
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hibernate","hibernate");  
/*(Here the parameter file should be in .jasper extension 
i.e., the compiled report)*/  
JasperPrint print = JasperFillManager.fillReport(  
stream, parameter, con);  
JRViewer viewer=new JRViewer(print);  
Container c=getContentPane();  
c.setLayout(new BorderLayout());  
c.add(viewer);  
}  
catch(ClassNotFoundException cnfe)  
{  
cnfe.printStackTrace();  
}  
catch(SQLException sqle)  
{  
sqle.printStackTrace();  
}  
catch(JRException jre)  
{  
jre.printStackTrace();  
}  
}  

   public MyiReportViewer(InputStream stream)  
{  
this(stream,null);  
}  


*(Another (calling) class)

 private void studentReportEnrActionPerformed(java.awt.event.ActionEvent evt) {                                                   
            String input = JOptionPane.showInputDialog("Enter the Student Enrollment No");  
            if (input != null) {  
                try {  
                    long enrollMentNo = Long.parseLong(input);  
                    HashMap parameters = new HashMap();  
                    System.out.println(enrollMentNo);  
                    parameters.put("EnrollmentNo", enrollMentNo);  
                    InputStream in = getClass().getResourceAsStream("reports/Student's Detail.jasper");  
                    MyiReportViewer myiReportViewer = new MyiReportViewer(in, parameters);  
                    myiReportViewer.setBounds(  
                            0, 0, desktopPane.getWidth(), desktopPane.getHeight());  
                    myiReportViewer.setVisible(true);  
                    desktopPane.add(myiReportViewer);  
                    myiReportViewer.setSelected(true);  
                } catch (PropertyVetoException pve) {  
                    pve.printStackTrace();  
                } catch (NumberFormatException nfe) {  
                    JOptionPane.showMessageDialog(  
                            this, "Please input numbers only");  
                }  
            }  
        }                    

This throws the following exception :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2266)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:57)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:197)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:380)
at sms.ui.ireport.MyiReportViewer.<init>(MyiReportViewer.java:53)
..................
..................

Line no 53 of MyiReportViewer is :
JasperPrint print = JasperFillManager.fillReport(
stream, parameter, con);

The stream passed in line no 53 is null. (verified by an if condition)

How should i correct it

Recommended Answers

All 18 Replies

Are you saying that the stream parameter passed to the MyiReportViewer constructor is null?
Can you look at the code that calls that method and see why?

Yes NormR1. Parameter passed to MyiReportViewer constructor is null...
This verifies it :

public MyiReportViewer(InputStream stream,HashMap parameter)
{
this();
try
{
    if(stream==null){
        System.out.println("Null stream");

    }
//some codes

Why does the caller of the method pass a null value?

The caller of this constructor passes a file as a stream

InputStream in = this.getClass().getClassLoader().getResourceAsStream("reports/StaffDetail.jasper");

                    MyiReportViewer myiReportViewer = new MyiReportViewer(
                            in, parameters);

The code should test that the file exists and that the value of in is not null before calling the method.

Then why is the value of in null?
To show where the program is looking for the file, create a File object with the path that is in the getResourceAsStream() method call and print out the File's absolute path.

Tried this :

File f = new File("reports/Student's Detail.jasper");
System.out.println(f.getAbsolutePath());

It prints
D:\HIbernate 26th April\FinalStudent\reports\Student's Detail.jasper
HIbernate 26th April- name of workspace
FinalStudent - name of project
reports folder contains all .jasper files

structure of the project (in IDE)

FinalStudent
--src
------org.nit.Entity
------org.nit.Dao
............
............
reports
----all .jasper files

You changed the file name:

this.getClass().getClassLoader().getResourceAsStream("reports/StaffDetail.jasper");

vs

File f = new File("reports/Student's Detail.jasper");

Did the print out show you anything?
Is the path that printed what you expected?

sorry. I changed the file name. It still gives the NullPointerException
and the file path is correct
D:\HIbernate 26th April\FinalStudent\reports\Student's Detail.jasper
D: is the location
HIbernate 26th April- name of workspace
FinalStudent - name of project
reports folder contains all .jasper files

If the file is where the program is looking for it and the spelling is correct, then the value of in should not be null.

But, the value is null. Spelling is also correct

Stranger thing is that, the code doesn't works in IDE, but, when i converted it into .exe, it works fine. All modules (even report) works fine. Still wonder , why?

The paths are different in the IDE.

Yes, different than jar. Is this the reason?

But still, i wonder, why it doesn't works in IDE

The getResource methods use the classpath. The IDE can set the classpath to be different from when you use the java command.

Okay..Thanks a lot NormR1. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.