How to sum 2 variables and store it into one varia

2019-02-28 09:18发布

问题:

I have a mainreport containing 2 different subreports.

I have a variable sum from a subreport1 for example 10(type Integer)

I have another variable sum from another subreport2 for example 2(type Integer)

I have successfully retreived the 2 variables from each of the subreports now I want to create a variable in my mainreport that sums up the 2 variables

so now in my mainreport I have 3 variables

the first variable is called VAR1 and it came from subreport 1

Name                       VAR1
Variable Class             java.lang.Integer
Reset type                 Report
initial Value Expression   0

the second variable is called VAR2 and it came from subreport 2

Name                       VAR2
Variable Class             java.lang.Integer
Reset type                 Report
initial Value Expression   0

the last variable is called SUM

Name                       SUM
Variable Class             java.lang.Integer
Reset type                 Report
variable expression        new Integer($V{VAR1}.intValue() + $V{VAR2}.intValue() )
initial Value Expression   0

The SUM variable is NULL when i preview the pdf , VAR1 and VAR2 are Integer values but the SUM variable is always NULL

could it be that the SUM was evaluated before VAR1 and VAR2?

what is my problem?

回答1:

Your suspicion is correct, using variables with values returned from subreports to calculate other variables does not work because "regular" variable values are computed before the subreports render and return values.

If you only need to sum the returned values, one thing that you can do is to return the value from the two subreports into the same variable, using calculation="Sum" for the return value. That is return val1 from the first subreport to VAR1 and the same val1 to SUM with calculation="Sum", and return val2 from the second subreport to VAR1 and to SUM with calculation="Sum".

Note that calculation="Sum" is for the return value and not the variable; variables that are used for return values would typically need to have calculation="System".