0

Using Jaspersoft Studio 6.4.

I am trying to create a java.util.Collection, with nested type java.lang.String.

I want to populate the collection with the values from my data query: iterate through the values of the Field $F{CostCenter} and add each value to my collection. (My query is a domain query).

I have tried

  1. Creating a collection variable
  2. Incrementing the variable by my CostCenter group
  3. Adding the field value to my variable

    <variable name="dls_CCArray" class="java.util.Collection" incrementType="Group" incrementGroup="CCGroup">
      <variableExpression><![CDATA[$V{dls_CCArray}.add( $F{costCenterSet.costCenterConcatenated} )]]> 
      </variableExpression>
    </variable>
    

But my variable is null, even though i know my query is returning cost centers.

Reason I need to do this: I have an optional input control. When i select no cost centers, i still need to pass the list of cost center values returned by the query to my next report through my hyperlink parameter.

Thanks in advance

Alex K
  • 22,315
  • 19
  • 108
  • 236
dharol
  • 151
  • 2
  • 17
  • 1
    The `Collection.add(T)` method returns boolean. You need to write expression to return the collection. You can write custom utility class and use it, for example. Or try to use Java 8 syntax or try to find ready to use libraries. [Append object to list and return result in Java 8](https://stackoverflow.com/questions/41070619/append-object-to-list-and-return-result-in-java-8). – Alex K Nov 23 '19 at 16:31

1 Answers1

1

You can use a second variable to add the value to the collection variable. Also, since the engine might evaluate variable expressions more than once, it would be safer to collect the values in a Set so that you don't end up with duplicate values.

Therefore you could have something like this:

<variable name="Values" class="java.util.Set" calculation="System">
    <initialValueExpression>new java.util.HashSet()</initialValueExpression>
</variable>
<variable name="ValueAdd" class="java.lang.Boolean">
    <variableExpression>$V{Values}.add($F{costCenterSet.costCenterConcatenated})</variableExpression>
</variable>
dada67
  • 4,723
  • 17
  • 19