我正在Netbeans中构建一个Java应用程序,所有东西都可以通过运行在IDE中运行,但是一旦我直接构建并运行jar,Jasper Reports就无法打开。 任何帮助将不胜感激,这让我疯狂
堆栈跟踪
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:56)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:277)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:262)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:415)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:838)
at membershipdb.MemberProgram.MembersReportButtonActionPerformed(MemberProgram.java:184)
at membershipdb.MemberProgram.access$000(MemberProgram.java:34)
at membershipdb.MemberProgram$1.actionPerformed(MemberProgram.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
守则
在membershipdb.MemberProgram.access $ 000(MemberProgram.java:34)
public class MemberProgram extends javax.swing.JFrame {
在membershipdb.MemberProgram $ 1.actionPerformed(MemberProgram.java:85)
MembersReportButtonActionPerformed(evt);
MembersReportButton.setText("<html><CENTER>MEMBER<BR>REPORT</HTML>");
MembersReportButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MembersReportButtonActionPerformed(evt);
}
});
在membershipdb.MemberProgram.MembersReportButtonActionPerformed(MemberProgram.java:184)
JasperPrint jp = JasperFillManager.fillReport(is, null, conn);
按钮的完整代码
private void MembersReportButtonActionPerformed(java.awt.event.ActionEvent evt) {
Connection conn = DBConnection.getConnection();
//Path to your .jasper file in your package
String reportName ="reports/MembersReport.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();
Component add = jf.getContentPane().add(jv);
jf.validate();
jf.setVisible(true);
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} catch (JRException ex) {
ex.printStackTrace();
}
}