使用JRBeanCollectionDataSource时,Jaspersoft Studio会抱怨类转换异常

时间:2014-06-18 09:34:13

标签: java jasper-reports jaspersoft-studio

我对 Jaspersoft Studio 和自定义JRBeanCollectionDataSource有一个奇怪的问题。

每次我尝试访问父对象的子对象时,都会出现这样的异常:

net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : 
    Source text : $F{test}.getFirst() + " " + $F{test}.getSecond()
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:466)
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$18(ReportControler.java:441)
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler$4.run(ReportControler.java:333)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : 
    Source text : $F{test}.getFirst() + " " + $F{test}.getSecond()
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:263)
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:611)
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:579)
    at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:1016)
    at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:504)
    at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:488)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:259)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:456)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillBandNoOverflow(JRVerticalFiller.java:467)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnHeader(JRVerticalFiller.java:512)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:294)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:152)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:963)
    at net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFiller.run(BaseFillHandle.java:120)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: mypacket.Test cannot be cast to mypacket.Test
    at Blank_A4_1403082997789_739077.evaluate(Blank_A4_1403082997789_739077:169)
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:250)
    ... 14 more

以下是相关课程:

public class RootFactory {

    public static JRBeanCollectionDataSource get() {
        return new JRBeanCollectionDataSource(Arrays.asList(new RootClass(new Test())));
    }
}


public class RootClass {
    private Test test;

    public RootClass(Test test) {
        this.test = test;
    }

    public Test getTest() {
        return test;
    }

    public void setTest(Test test) {
        this.test = test;
    }
}


public class Test {

    private String first = "first";
    private String second = "second";

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }
}

我已经为 Jaspersoft Studio 添加了类,并添加了数据适配器以使用RootFactory.get()方法,这似乎正在发挥作用。

jasper中的字段表达式是$ F {test} .getFirst()+“”+ $ F {test} .getSecond()

奇怪的是,jasper无法将mypacket.Test转换为mypacket.Test。

任何线索如何解决这个问题?

- Jaspersoft studio 5.6.0
- Java 1.7.0_11

修改1

这是jrxml文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version last-->
<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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a333429d-13c8-4db0-849e-9ed3fa97af6e">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="TempFactory"/>
<queryString>
    <![CDATA[]]>
</queryString>
<field name="test" class="mypacket.Test"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="79" splitType="Stretch"/>
</title>
<pageHeader>
    <band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="61" splitType="Stretch">
        <textField>
            <reportElement x="0" y="0" width="555" height="61" uuid="0ca07834-5483-4ad5-a7fc-b415c2cce367"/>
            <textFieldExpression><![CDATA[$F{test}.getFirst() + " " + $F{test}.getSecond()]]></textFieldExpression>
        </textField>
    </band>
</columnHeader>
<detail>
    <band height="125" splitType="Stretch"/>
</detail>
<columnFooter>
    <band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>
</jasperReport>

如果它确实重要:我使用osx 10.9.2。

2 个答案:

答案 0 :(得分:3)

我在5.5.2和5.6中都遇到了类似的例外,比如...无法从T转换为T ...其中T总是a)完全相同的类型b)自己的pojo派生的fr JRAbstractBeanDataSource

我知道这不是一个真正的解决方案,并认为这可能应该提交给Jasper家伙......但只是简单地“重启jasper工作室”总是有帮助: - / 工作室里的许多小事之一...

希望它有点帮助

答案 1 :(得分:0)

重新启动Jasper Studio对我不起作用。

我认为这仍然是Jasper Studio 6.0.4独立版本中的一个错误。但是当我将Studio软件安装为Eclipse插件并从Java项目中预览报表时,它可以正常工作而没有任何问题。

好处是我可以将报告设置为与Jasper Reports版本4.5.0向后兼容,因此每次我预览报告时它都不会在整个报告中创建UUID元素,而eclipse插件也适用于Java 8 SDK