我使用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