4

I have a weird problem with Jaspersoft Studio and custom JRBeanCollectionDataSource.

Every time I try to access child object of parent object, I get exception like this:

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

Here is relevant classes:

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;
    }
}

I've added classes to Jaspersoft Studio and added data adapter to use RootFactory.get() method and that seems to be working.

Field expression in jasper is $F{test}.getFirst() + " " + $F{test}.getSecond()

Weird thing is that jasper can't cast mypacket.Test to mypacket.Test.

Any clues how to solve this?

- Jaspersoft studio 5.6.0
- Java 1.7.0_11

Edit 1

Here is jrxml file

<?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>

And if it does matter: I use osx 10.9.2.

Lagopus
  • 66
  • 1
  • 5

3 Answers3

4

I have faced similar exceptions in both 5.5.2 and 5.6, like ...cannot cast from T to T... where T always was a) the exact same type b) an own pojo derived fr JRAbstractBeanDataSource

I know this is not really a solution and think this probably should be filed to the Jasper guys... but just plain "restarting jasper studio" always helped :-/ One of many little things in studio...

Hope it kinda helps nonetheless

ton
  • 41
  • 1
0

Restarting Jasper Studio did not work for me.

I think this is still a bug in Jasper Studio 6.0.4 standalone version. But when I install Studio software as an Eclipse plugin and preview the report from the Java project, it works fine without any problems.

The good thing is I can set the report to be backward compatible with Jasper Reports version 4.5.0 so it does not create the UUID elements all over the report every time I preview the report and the eclipse plugin also works with Java 8 SDK

Krishna
  • 3
  • 6
0

For anyone coming to this thread and and still having the issue, I managed to solve it in my case, by doing so:

  • Right-click on the project -> "Build Path" -> "Configure Build path..."
  • Navigate to "Order and Export" tab
  • Uncheck "Jaspersoft Server Library" ("JRE System Library" must be checked though)
  • Apply and close
  • Restart Jasperstudio
  • Go back to "Preview" and rerun the generation of the report
  • Enjoy!

enter image description here

In fact, the problem was that different classes loaders were used to load the same class T:

  • The default classloader from "JRE System Library"
  • The one from "Jaspersoft Server Library"

You can confirm this by looking the error stacktrace in full (notice the different instances of the classloader, in bold):

Caused by: java.lang.ClassCastException: class <full.package.to.class.T> cannot be cast to class <full.package.to.class.T>(<full.package.to.class.T> is in unnamed module of loader java.net.FactoryURLClassLoader @7fe379ab; <full.package.to.class.T> is in unnamed module of loader java.net.FactoryURLClassLoader @4b9b57d9)

tinesoft
  • 766
  • 8
  • 13