Please or Register to create posts and topics.

Variable executed twice?

Hello all,

i was trying to use a variable to build a StringBuilder and got strange results. I found out at the end that my variable is executed twice.

I see someone else had this question https://stackoverflow.com/questions/29799964/user-defined-variables-are-executed-twice-in-jasper but there are no answers. minimum example with jasperreports:7.0.0:

<dataset name="Dataset1" uuid="ca5e5b23-f977-459d-92ae-0d3378d3b12a">
<query language="sql"><![CDATA[]]></query>
<variable name="twice" class="java.lang.String">
<expression><![CDATA["value";System.out.println("value")]]></expression>
<initialValueExpression><![CDATA["init";System.out.println("init")]]></initialValueExpression>
</variable>
</dataset>

...

<title height="79" splitType="Stretch">
<element kind="component" uuid="4ce41c26-614b-4293-88ba-05cb99431db0" x="0" y="0" width="555" height="79">
<component kind="list" printOrder="Vertical">
<datasetRun subDataset="Dataset1" uuid="9c61b4b0-4dcc-4e43-a251-5aa3410cb144">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Arrays.asList("foo", "bar", "baz"))]]></dataSourceExpression>
</datasetRun>
<contents height="79" width="555"/>
</component>
</element>
</title>

Then I run it as

JasperPrint report = JasperCompileManager.compileReport("variable-twice.jrxml");
JasperPrint print = JasperFillManager.fillReport(report, null)

then I get the output

value
init
value
value
value
value
value

I dont know why it is executing the variable twice. Does someone know a solution?