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