I'm trying to display a report in my src/iReport/report1.jasper directory. but every time i try to display it i get java.lang.NullPointerException. I can't find where the problem is. Please someone just take a look and help me. I have imported all necessary libraries. im using Netbeans.
package Windows;
import java.awt.Dimension;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;
public class Report1 extends javax.swing.JFrame {
Connection conn = null;
public void initConnection(){
String HOST = "jdbc:mysql://localhost:3306/StudentDB";
String USERNAME = "root";
String PASSWORD = "";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
try {
conn = DriverManager.getConnection(HOST, USERNAME, PASSWORD);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void showReport(){
//Path to your .jasper file in your package
String reportName = "iReports\report1.jasper";
//Get a stream to read the file
InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName);
try {
//Fill the report with parameter, connection and the stream reader
JasperPrint jp = JasperFillManager.fillReport(is, null, conn);
//Viewer for JasperReport
JRViewer jv = new JRViewer(jp);
//Insert viewer to a JFrame to make it showable
JFrame jf = new JFrame();
jf.getContentPane().add(jv);
jf.validate();
jf.setVisible(true);
jf.setSize(new Dimension(800,600));
jf.setLocation(300,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (JRException ex) {
ex.printStackTrace();
}
}
/** Creates new form Report1 */
public Report1() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addContainerGap(327, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addContainerGap(277, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
initConnection();
showReport();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Report1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}