Variable executed twice?
Quote from jose-da-s on 18. March 2026, 13:35Hello 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
valueI dont know why it is executing the variable twice. Does someone know a solution?
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?
Quote from jasperman on 19. March 2026, 22:15Hi Jose,
Indeed, variables are or might be evaluated twice per record; this makes pure sense in many, normal scenarios, for example following example:
Variable 1 = Area of rectangle R
Variable 2 = Hight of rectangle R, calculated from fields
Variable 3 = Width of rectangle R, calculated from fields
Would the variables 1, 2, 3 only be evaluated once in that order, the area in Variable 1 would always be wrong. Her it makes absolutely sense to evaluate twice.
Using variables for System.out or list operations is not recommended, we should use Scriptlet and real programming for such complex needs in general.
But in the end it is of cause possible to manage it; instead of an array (where you insert an element twice), one could perhaps use a HashMap (where it doesn't matter when the same element is inserted twice). Control your System.out with two helper variables, which count records, one before your System.out variable, one after. In your Ssystem.out variable, print the output only if before variable value is not equal to after variable value (in the first evaluation phase only the before variable is incremented and therefore has a different value than the after variable. In the second evaluation phase, the variable values should be the same...)
Pls. let me know if you tried and what solution works best for you!
Thanks + Greetings
Hi Jose,
Indeed, variables are or might be evaluated twice per record; this makes pure sense in many, normal scenarios, for example following example:
Variable 1 = Area of rectangle R
Variable 2 = Hight of rectangle R, calculated from fields
Variable 3 = Width of rectangle R, calculated from fields
Would the variables 1, 2, 3 only be evaluated once in that order, the area in Variable 1 would always be wrong. Her it makes absolutely sense to evaluate twice.
Using variables for System.out or list operations is not recommended, we should use Scriptlet and real programming for such complex needs in general.
But in the end it is of cause possible to manage it; instead of an array (where you insert an element twice), one could perhaps use a HashMap (where it doesn't matter when the same element is inserted twice). Control your System.out with two helper variables, which count records, one before your System.out variable, one after. In your Ssystem.out variable, print the output only if before variable value is not equal to after variable value (in the first evaluation phase only the before variable is incremented and therefore has a different value than the after variable. In the second evaluation phase, the variable values should be the same...)
Pls. let me know if you tried and what solution works best for you!
Thanks + Greetings
