我正在尝试使用jasper报告打印到pdf文件;从数据库中获取的结果列表。现在,当程序尝试生成报告时,我收到了空指针异常/格式错误的URL错误。
我已经运行了这段代码几次,并且以前曾经工作过,但现在不行了。我正在Ubuntu盒子(18.04)上运行它。我已经尝试调试并尝试更改代码几次。我还运行了一些system.out一些地方,以查看输入是否通过。
@GetMapping(value = "/salary/reports/{id}/{month}")
public void report(HttpServletResponse response, @PathVariable
Long id, @PathVariable Long month) throws Exception {
response.setContentType("text/html");
System.out.println(id+"---------"+month);
JRBeanCollectionDataSource dataSrc = new JRBeanCollectionDataSource(reportGenerationServiceImpl.generateMonthlyReport(id, month));
//JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(reportGenerationServiceImpl.generateMonthlyReport(id, month));
InputStream inputStream = this.getClass().getResourceAsStream("/reports/payslip.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSrc);
try {
File pdf = File.createTempFile("payslip", ".pdf",new File("/home/foo/Documents"));
JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pdf));
HtmlExporter exporter = new HtmlExporter(DefaultJasperReportsContext.getInstance());
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter()));
exporter.exportReport();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="payslip" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" whenResourceMissingType="Empty" uuid="d8f2655e-ef65-49cc-90cc-a08385cad0fd">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="ReportDataSet" uuid="193ed9cd-3d2b-4858-9cca-d21a951019a2">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Mysql Data Adapter "/>
<queryString language="SQL">
<![CDATA[Select * from payslip.salary]]>
</queryString>
<field name="id" class="java.lang.Long"/>
<field name="name" class="java.lang.String"/>
<field name="basic" class="java.lang.Double"/>
<field name="hra" class="java.lang.Double"/>
<field name="otherAllowances" class="java.lang.Double"/>
<field name="taxDeductions" class="java.lang.Double"/>
<field name="netSalary" class="java.lang.Double"/>
<field name="month" class="java.lang.String"/>
</subDataset>
<background>
<band height="1" splitType="Stretch"/>
</background>
<title>
<band height="123" splitType="Stretch">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<rectangle>
<reportElement mode="Opaque" x="140" y="0" width="415" height="110" backcolor="#0C4ACF" uuid="2575eb12-f718-4332-a349-e5fbeee62958"/>
<graphicElement>
<pen lineColor="rgba(0, 0, 0, 0.0)"/>
</graphicElement>
</rectangle>
<image>
<reportElement x="0" y="0" width="140" height="110" uuid="32ad2748-1f14-4ac5-a9df-91670ab327d4"/>
<imageExpression><![CDATA["logo.png"]]></imageExpression>
</image>
<staticText>
<reportElement x="430" y="0" width="125" height="110" forecolor="#F0F0F0" uuid="97d884db-2ed2-49e3-a4fa-652031fbcc59"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SansSerif" size="14" isBold="true"/>
</textElement>
<text><![CDATA[Confidential]]></text>
</staticText>
<staticText>
<reportElement x="140" y="0" width="290" height="50" forecolor="#F0F0F0" uuid="386c4505-43e3-47d6-8893-3fd8faa150c5"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="18"/>
</textElement>
<text><![CDATA[Payslip Report]]></text>
</staticText>
<textField>
<reportElement x="240" y="50" width="100" height="30" forecolor="#F0F0F0" uuid="323333dd-7e41-4518-816d-e765c5e9c6d6"/>
<textFieldExpression><![CDATA[$F{month}]]></textFieldExpression>
</textField>
</band>
</title>
<columnHeader>
<band height="29" splitType="Stretch">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="240" height="29" forecolor="#F0F0F0" backcolor="#0C4ACF" uuid="51ec09f9-1c6d-45c8-bcae-4c4c395621b7"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<text><![CDATA[Description]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="240" y="0" width="160" height="29" forecolor="#F0F0F0" backcolor="#0C4ACF" uuid="06e52895-7d44-4c5b-aec4-9a1a8315b04b"/>
<box>
<leftPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<text><![CDATA[Salary]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="400" y="0" width="155" height="29" forecolor="#F0F0F0" backcolor="#0C4ACF" uuid="f2eddb9e-c8ca-4061-bf3e-4ee15f0df991"/>
<box>
<leftPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<text><![CDATA[Deductions]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="231" splitType="Stretch">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<frame>
<reportElement x="0" y="0" width="555" height="231" uuid="78d0820a-8cbd-4536-8b9a-a2efaaca7df5"/>
<line>
<reportElement x="399" y="0" width="1" height="231" uuid="9bdaf357-20d3-4148-977a-ce68e7ddef06"/>
</line>
<line>
<reportElement x="240" y="0" width="1" height="232" uuid="406a057f-6d73-4830-8689-d378c4e0ba68"/>
</line>
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="d0e0164b-793c-4fe9-9e2d-e2f1aeeff9fb"/>
<text><![CDATA[Basic Salary]]></text>
</staticText>
<textField>
<reportElement x="241" y="0" width="100" height="30" uuid="94a0e1bc-9650-45b1-8a52-2896568eaf1c"/>
<textFieldExpression><![CDATA[$F{basic}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="30" width="100" height="30" uuid="e59e744f-e514-48bb-b47e-d8d06b7db272"/>
<text><![CDATA[HRA]]></text>
</staticText>
<textField>
<reportElement x="240" y="30" width="100" height="30" uuid="9510ee9e-3938-48a6-9242-0844eb99e705"/>
<textFieldExpression><![CDATA[$F{hra}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="62" width="100" height="30" uuid="946bcc0f-9730-4881-abd4-fdbbf6ca9e8d"/>
<text><![CDATA[Other Allowances]]></text>
</staticText>
<textField>
<reportElement x="240" y="62" width="100" height="30" uuid="3c55b1e5-4a62-417e-838c-f940d354b553"/>
<textFieldExpression><![CDATA[$F{otherAllowances}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="90" width="100" height="30" uuid="90ee3fc0-fca8-4b8d-8eed-7abdd3f80974"/>
<text><![CDATA[Tax Deductions]]></text>
</staticText>
<textField>
<reportElement x="400" y="97" width="100" height="30" uuid="8c36f0c9-2053-4231-a23b-6501afd9285a"/>
<textFieldExpression><![CDATA[$F{taxDeductions}]]></textFieldExpression>
</textField>
<rectangle>
<reportElement mode="Opaque" x="-1" y="207" width="556" height="25" forecolor="#F0F0F0" backcolor="#0C4ACF" uuid="816a2afe-9b04-46ef-99a4-ce3bae3530e3"/>
</rectangle>
<staticText>
<reportElement x="0" y="210" width="241" height="21" forecolor="#F0F0F0" uuid="8b64e3c7-af3d-44fe-9702-8c51f0e5011f"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<text><![CDATA[Net Salary]]></text>
</staticText>
<textField>
<reportElement x="399" y="207" width="156" height="24" forecolor="#F0F0F0" uuid="b88e8ba1-ab37-4548-b606-766516b4ce8f"/>
<textFieldExpression><![CDATA[$F{netSalary}]]></textFieldExpression>
</textField>
<line>
<reportElement x="-1" y="232" width="556" height="1" uuid="1d9c5abd-5b53-4aa5-bde9-5f690d74ac8f"/>
</line>
</frame>
</band>
</detail>
<pageFooter>
<band height="54" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="340" y="0" width="215" height="30" uuid="a3d54f19-0e4c-48f8-a01b-0436f3cb8a78"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page " + $V{PAGE_NUMBER} + " of " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
虽然我应该得到一个包含结果的pdf文件,但结果却是这样的:
1--------------------------9
java.lang.NullPointerException: null
at java.net.URL.<init>(URL.java:532) ~[na:1.8.0_191]
at java.net.URL.<init>(URL.java:490) ~[na:1.8.0_191]
at java.net.URL.<init>(URL.java:439) ~[na:1.8.0_191]
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:620) ~[na:1.8.0_191]
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148) ~[na:1.8.0_191]
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:806) ~[na:1.8.0_191]
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771) ~[na:1.8.0_191]
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) ~[na:1.8.0_191]
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) ~[na:1.8.0_191]
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) ~[na:1.8.0_191]
at org.apache.commons.digester.Digester.parse(Digester.java:1892) ~[commons-digester-2.1.jar:2.1]
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:297) ~[jasperreports-6.0.0.jar:6.0.0]
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:284) ~[jasperreports-6.0.0.jar:6.0.0]
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:273) ~[jasperreports-6.0.0.jar:6.0.0]
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:247) ~[jasperreports-6.0.0.jar:6.0.0]
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:338) ~[jasperreports-6.0.0.jar:6.0.0]
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:605) ~[jasperreports-6.0.0.jar:6.0.0]
at org.synprox.controller.SalaryController.report(SalaryController.java:74) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
答案 0 :(得分:0)
经过更多调试后,我自己解决了问题。 这是我的方法。调试后,错误似乎出在
InputStream inputStream = this.getClass().getResourceAsStream("/reports/payslip.jrxml");
因此,在查看导入时,我发现我错误地导入了jasper报告库,而应该将其导入java.io.InputStream
。