我使用Jasper Reports(4.0.0)爲我們的項目(JRE 1.6.x)創建報告。在NullPointerException中取消Jasper報告生成
我在我的腳本中遇到NullPointerException
s,因爲它們不會導致報告中止,他們只是將該字段默認評估爲null
。據我測試,它只發生在NPE的。
生成的報告非常重要,不應該有任何錯誤。所以,如果發生異常(像NPE的某些缺失數據),它應該停止生成,並且數據庫和/或報告應該被修復。這種行爲可能會隱藏一些報告編碼錯誤,除非有人發現缺失的字段。
NPE的這種行爲的原因是什麼?是否有任何編譯選項或執行標誌可以切換到使NPE的泡沫上升,如同其他例外情況一樣? 我真的希望我不必在try..catch
塊中包裝每個scriptlet方法,並在NPE上引發另一個異常。
謝謝!
好吧,樣本報告。它不使用小腳本,而是直接在字段評價(結果是一樣的)生成NPE,並且可以不帶參數上iReport的預覽直接執行:
<?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="NPE_Report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<variable name="NullVariable" class="java.lang.Integer" calculation="System">
<initialValueExpression><![CDATA[null]]></initialValueExpression>
</variable>
<variable name="NotNullVariable" class="java.lang.Integer" calculation="System">
<initialValueExpression><![CDATA[200]]></initialValueExpression>
</variable>
<title>
<band height="72" splitType="Stretch">
<staticText>
<reportElement x="11" y="10" width="179" height="22"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[Successful field evaluation:]]></text>
</staticText>
<textField>
<reportElement x="190" y="10" width="296" height="22"/>
<textElement>
<font size="14"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA["0x"+Integer.toHexString($V{NotNullVariable}.intValue()+55)]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="11" y="32" width="179" height="22"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[NPE on field evaluation:]]></text>
</staticText>
<textField>
<reportElement x="190" y="32" width="296" height="22"/>
<textElement>
<font size="14"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA["0x"+Integer.toHexString($V{NullVariable}.intValue()+55)]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="24" splitType="Stretch"/>
</detail>
</jasperReport>
分享一些代碼。 – 2011-04-05 03:48:46
@Dave Jarvis代碼添加 – mdrg 2011-04-05 12:17:05