Why is a calculation result null when done in a variable, but not when done in a text field?

I'm using JS Studio 6.4.0 and Jasper Reports library 6.4.1 in a Java application. I have a master report with 2 subreports embedded in the summary. The master calculates 2 values (loggingTotal, truckingTotal). The subreports return one value each (expenseTotal, logTotal).

When I create a variable (subTotal) to add these together and I display that variable in a text field, the value is null.

When I perform the addition within the text field itself, the value is correct.

Why do these displayed values differ? How can I store the subTotal calculation in a variable for later reuse?

The only difference between the working and non-working version is that this line:

                <textFieldExpression><![CDATA[$V{logTotal}.subtract($V{truckingTotal}).subtract($V{loggingTotal}).subtract($V{expenseTotal})]]>

is replaced with this one:

                <textFieldExpression><![CDATA[$V{subTotal}]]></textFieldExpression>

Applicable jrxml for the working version is included below.

    <variable name="expenseTotal" class="java.math.BigDecimal"/>
    <variable name="logTotal" class="java.math.BigDecimal"/>
    <variable name="truckingTotal" class="java.math.BigDecimal" resetType="None">
        <variableExpression><![CDATA[$V{truckingAmt1}]]></variableExpression>
    </variable>
    <variable name="loggingTotal" class="java.math.BigDecimal" resetType="None">
        <variableExpression><![CDATA[$V{loggingAmt1}]]></variableExpression>
    </variable>
    <variable name="subTotal" class="java.math.BigDecimal">
        <variableExpression><![CDATA[$V{logTotal}.subtract($V{truckingTotal}).subtract($V{loggingTotal}).subtract($V{expenseTotal})]]></variableExpression>
    </variable>

....

            <subreport>
                <reportElement positionType="Float" x="0" y="60" width="572" height="40" uuid="d3ec84fb-528f-41f5-8b54-26112fd95b50"/>
                <subreportParameter name="loadList">
                    <subreportParameterExpression><![CDATA[$P{loadList}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <returnValue subreportVariable="logTotal" toVariable="logTotal"/>
                <subreportExpression><![CDATA["scaling/reports/ScaleSummary_Logs.jasper"]]></subreportExpression>
            </subreport>
            <subreport>
                <reportElement positionType="Float" x="0" y="30" width="572" height="20" isRemoveLineWhenBlank="true" uuid="18231cd1-cdfa-4733-bbaa-7ba21f6708cb"/>
                <subreportParameter name="loadList">
                    <subreportParameterExpression><![CDATA[$P{loadList}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <returnValue subreportVariable="expenseTotal" toVariable="expenseTotal"/>
                <subreportExpression><![CDATA["scaling/reports/ScaleSummary_Expenses.jasper"]]></subreportExpression>
            </subreport>

...

            <textField evaluationTime="Auto" pattern="¤#,##0.00;-¤#,##0.00">
                <reportElement positionType="Float" x="492" y="125" width="80" height="11" uuid="a945c90c-5db9-4c29-8cca-852b0e081af2">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    <printWhenExpression><![CDATA[$V{loggingTotal} != null]]></printWhenExpression>
                </reportElement>
                <textElement textAlignment="Right">
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{loggingTotal}.negate()]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Auto" pattern="¤#,##0.00;-¤#,##0.00">
                <reportElement positionType="Float" x="492" y="136" width="80" height="11" uuid="607f10b8-5561-45e4-876c-30ab6e53cb23">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    <printWhenExpression><![CDATA[$V{truckingTotal} != null]]></printWhenExpression>
                </reportElement>
                <textElement textAlignment="Right">
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{truckingTotal}.negate()]]></textFieldExpression>
            </textField>

            <textField evaluationTime="Auto" pattern="¤#,##0.00;-¤#,##0.00">
                <reportElement positionType="Float" x="492" y="147" width="80" height="11" uuid="bf1a30c2-2580-44c1-9c54-668716647954">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Right">
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{expenseTotal}.negate()]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Auto" pattern="¤#,##0.00;-¤#,##0.00">
                <reportElement positionType="Float" x="492" y="114" width="80" height="11" uuid="bf1a30c2-2580-44c1-9c54-668716647954">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Right">
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{logTotal}]]></textFieldExpression>
            </textField>

            <textField evaluationTime="Report" pattern="¤#,##0.00;-¤#,##0.00">
                <reportElement positionType="Float" x="492" y="158" width="80" height="11" uuid="6d5b8d4f-d940-41e6-ae3a-a8bc0f89907a">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <box>
                    <topPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Right">
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{logTotal}.subtract($V{truckingTotal}).subtract($V{loggingTotal}).subtract($V{expenseTotal})]]></textFieldExpression>
            </textField>

mike_64's picture
86
Joined: Oct 30 2015 - 11:08am
Last seen: 4 years 11 months ago

@ReportDev: I understand that is the case, yes. However, I don't know what these values should be set to, and my attempts to find the correct values by trial and error have failed.

Both the subreports and the calculation are embedded in the Summary band.

I use increment type None for all variables in the subtotal calculation, since none of them use a calculation function (sum, avg, count, etc.)

I also used reset type Report for all variables, including subTotal, since these values should all be calculated at the end of the report fill phase.

In addition, I've used values Report, Master and Auto for the text box evaluation time.

Since I still see null, I'm at a loss as to what values I should be using here.

Suggestions?

mike_64 - 5 years 7 months ago

3 Answers:

The difference that you have observed is because of the Calculation being done.

When you created a text field, the calculation was being performed inside that particular band (either group or column footer)

When you  created a variable, the calculation can be performed at various levels and jasper provides multiple configuration options for these calculations. If the variable configuration is not properly done, it would result in a null value. The increment type and reset type are the main options that you should be selecting carefully on a variable. 

 

reportdev's picture
10554
Joined: Oct 12 2015 - 12:05pm
Last seen: 9 months 1 week ago
mike_64's picture
86
Joined: Oct 30 2015 - 11:08am
Last seen: 4 years 11 months ago

I think the difference betwen perform the calculation in a text field vs in a variable is clear, but...how can we fix this problem? Does it means it's not possible to perform this kind of calculations using variables? because currently Jasper doesnot allow to decide when a variable should be evaluated (as well as the text fields)

For me it seems more like a Jasper issue, because I have defined two variables, when I display them in a textfield it's ok, but when I use a third variable, and the only calculation that it has is a sum  operation (+) of those two variables, the result is null. IIf the problem is because some level of the variable calculations, then it should display null for some of them in the first scenario, however I think that is not the case and I'm prety sure that this is a Jasper issue.

Does anyone have any other suggestion?

garyantoriano's picture
Joined: Jun 7 2017 - 2:34pm
Last seen: 2 years 7 months ago
Feedback