JasperReports counter variable always incrementing

counterireportjasper-reportsreportvariables

This should be a simple question on JasperReports. I'm trying to do a simple counter over the whole report that should increment based on a condition. However, whatever I try, it seems like the counter variable is always being incremented, regardless of the variable expression. My variable's definition properties are below:

Class: Integer
Calculation: Count
Reset type: Report
Increment type: None
Variable Expression: $F{on_target}.doubleValue() >= 0.0
Initial Value: Integer.valueOf(0)

I have a total of 23 rows in the data set, and based on the criteria, the counter should eventually equal 18. I have the variable outputting in the Summary band, with Evaluation Time to Now. However, regardless of the evaluation time, and even setting the Variable Expression to Boolean.valueOf(true == false), the variable's value always ends up as 23.

What simple little thing am I forgetting?

Best Answer

I think I've got it. This makes vaguely no sense, but... (mind you, this is my first time working with Jasper Variables, so it was trial and error).

The Variable Expression isn't quite a Boolean, where a counter type variable isn't incremented if the expression is false, like you'd think. The variable is incremented if there is any value evaluated in the expression. Thus, for me, what ended up working is below:

$F{on_target} >= 0 ? 1 : null

Note the usage of null if the expression should be false.

It makes vague, twisted sense. But is in no way intuitive. Oh well, so it goes...

or in other words:

When you are using the Calculation:Count function of a Jasper-defined Variable you want the Variable Expression to:

  • resolve to non-null value to increment the counter
  • resolve to a null value if you do not want to increment the counter

That's why the test listed above works