0

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

2
Contributors
18
Replies
21
Views
4 Years
Discussion Span
Last Post by Kunal Lakhani
0

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?

Edited by NormR1

0

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
0

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);
0

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

0

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.

0

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

0

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?

0

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

0

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.

0

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?

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.