我已经创建了如下的 report3.jrxml 文件
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report3" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="872370a6-8d31-49c9-871a-8a5a0173946f">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[SELECT * FROM eposv1orders]]>
</queryString>
<field name="ID" class="java.lang.Integer">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="LineID" class="java.lang.Integer">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="OID" class="java.lang.Integer">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="OItem" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="OUnit" class="java.lang.Integer">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="OPrice" class="java.lang.Double">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="OAmount" class="java.lang.Double">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="163" y="10" width="178" height="54" uuid="5fea8912-8166-46d3-91b3-7972f00698f1"/>
<textElement>
<font size="36" isBold="true"/>
</textElement>
<text><![CDATA[ANETSYS]]></text>
</staticText>
<textField pattern="dd/MM/yyyy">
<reportElement x="367" y="49" width="133" height="30" uuid="0dff4ebe-ad4b-4a7f-b7ff-a6c4a44324bb"/>
<textElement>
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="111" height="20" uuid="4b604add-ebee-4a43-8673-10ce4779fd20"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[OItem]]></text>
</staticText>
<staticText>
<reportElement x="111" y="0" width="111" height="20" uuid="c953ebe8-4a50-4e8b-bc0c-e466152ba1c3"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[OID]]></text>
</staticText>
<staticText>
<reportElement x="222" y="0" width="111" height="20" uuid="43c0a817-ead2-4533-9d42-ec294d398bfe"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[OUnit]]></text>
</staticText>
<staticText>
<reportElement x="333" y="0" width="111" height="20" uuid="9499195b-8afc-4aef-a43f-8e02ab913266"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[OPrice]]></text>
</staticText>
<staticText>
<reportElement x="444" y="0" width="111" height="20" uuid="e53372c8-5dfc-483c-a093-50ef3af56be4"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[OAmount]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="28" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="111" height="20" uuid="128898e2-3e2c-4e16-8c3b-059ff8efbd66"/>
<textFieldExpression><![CDATA[$F{OItem}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="111" y="0" width="111" height="20" uuid="04f373fc-9df6-4a0e-9b98-4de30f8be885"/>
<textFieldExpression><![CDATA[$F{OID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="222" y="0" width="111" height="20" uuid="91cb7831-ec26-4324-9151-7e8ae338b9e0"/>
<textFieldExpression><![CDATA[$F{OUnit}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="333" y="0" width="111" height="20" uuid="a8466205-7da4-4419-810a-875f93da2929"/>
<textFieldExpression><![CDATA[$F{OPrice}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="444" y="0" width="111" height="20" uuid="94c05675-15af-45cc-8f5a-c218e59a1457"/>
<textFieldExpression><![CDATA[$F{OAmount}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
和 PrintReport.java 文件在此处;
package eposv1;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.swing.JRViewer;
public class PrintReport extends JFrame {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
public PrintReport() throws HeadlessException {
}
public void showReport() throws SQLException{
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/eposv1","root","");
String jasperReport = "C:/Users/soyut_2/Documents/etBeansProjects/EPOSv1/src/eposv1/report3.jrxml";
InputStream is = this.getClass().getClassLoader().getResourceAsStream(jasperReport);
//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();
}
}
}
而且,这是我单击按钮以显示报告的地方。
@FXML
public void showInvoice(){
try {
PrintReport viewReport = new PrintReport();
viewReport.showReport();
} catch (SQLException ex) {
Logger.getLogger(OrdersFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
我还为按钮分配了 showInvoice()功能
但是,以某种方式我遇到了这个错误。谁能告诉我这个错误代码显示什么,我在哪里犯错误? 这是错误代码; (错误代码已编辑,删除.zip文件后添加了新的.jar文件)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2338)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2351)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2822)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:301)
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:441)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:860)
at eposv1.PrintReport.showReport(PrintReport.java:55)
at eposv1.OrdersFXMLController.lambda$initialize$0(OrdersFXMLController.java:177)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
谢谢。