Lists & Collections as variable class

1

I have a need to save data in a List or Collection variable (of Integers).  I pass the variable to a scriptlet and then add in the data.  I believe my problem lies in the process of first instantiating this list/collection.  How to I do this before passing it on to the scriptlet and storing the information?

Thanks for any help.

 

tomptucker's picture
Joined: Jul 21 2010 - 8:19am
Last seen: 2 years 11 months ago

6 Answers:

1

I figured it out... for those of you trying to use collections and lists in your iReports... (very powerful), make sure to (1) set you Variable up iwth the correct [Calculation] ("System") and (2) make sure that the [Reset Type] is "Report", otherwise, if the [Reset Type] is "None", the collection/list never gets instantiated ! !

tomptucker's picture
Joined: Jul 21 2010 - 8:19am
Last seen: 2 years 11 months ago
1
Also.... make sure to add something like "new ArrayList()" in the [Initial Value Expression] field of the variable !..
tomptucker's picture
Joined: Jul 21 2010 - 8:19am
Last seen: 2 years 11 months ago
0

How do I add the values from a result set field to a collection variable?

I am trying to use a collection within my report - I have successfully used a collection filled by a multi-select input control, and that works fine.

This time, I need to make an outer shell or master report, and call my subreport with different parameters on each page, with the parameter coming from a dynamically-generated collection rather than an input control.

As a small test, I selected a few values from a field on a table, along with 'Page1', like this:

PageNum    Selection

page1           INB

page1           PRV

page1           PUB

 

I want to collect the different values in $F{Selection} into a collection, to pass to my subreport.

I created a variable like this, with the reset type = Report :

    <variable name="variable1" class="java.util.Collection" calculation="System">
        <variableExpression><![CDATA[$F{Programs}]]></variableExpression>
        <initialValueExpression><![CDATA[new ArrayList()]]></initialValueExpression>
    </variable>
 

my idea is that the master report will group by PageNum, and start each PageNum group on a new page, with the call to the subreport in the Group Footer, passing the collection of Selection values in a single parameter (I would have the reset on the variable be at the group level).

So far I haven't even put the subreport in yet, I just have the simple query that gets those sample values, the fields, and the variable that I'm trying to populate.

If I hit "compile" it doesn't give me any errors, but when I try to Preview I get:

Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'INB' with class 'java.lang.String' to class 'java.util.Collection'

I have a feeling I need to use something within the variable expression, but I am not a java programmer so I don't really know what to use - can anyone advise?

jrot's picture
29
Joined: Apr 8 2010 - 12:30pm
Last seen: 9 years 6 months ago

Your problem is that you are trying to assign a string to a Collection resulting in the error. Here's what will solve it
1. use class="java.util.ArrayList" (instead of Collection, which is an interface)
2. use $V{variable1}.add($F{Programs}) in your variable Expression. What this does is adds the value of INB etc. to the list, instead of trying to assign the string INB to the collection (or ArrayList). That should take care of the class cast error.

max_max_mir - 5 years 1 month ago

This worked for me and is a HUGE HELP.

srang - 4 years 5 months ago
0

 Have you resolve it yet?

I have encountered a situation just like what you did.

jjjjc's picture
2
Joined: May 29 2012 - 5:15am
Last seen: 7 years 4 months ago
0
Have you resolve it yet?
I have encountered a situation just like what you did.
jjjjc's picture
2
Joined: May 29 2012 - 5:15am
Last seen: 7 years 4 months ago

Hi
Do you find the solution ?
I really need it.

Thanks!

dox102000 - 6 years 9 months ago
0

Here are a few things you can do

  1. Use java.util.ArrayList (instead of java.util.Collection). While it's advisable to use the latter in programming, in reports, using a concrete class lets you use the classes specific methods (if you use a Collection, you can only use the functions it has, and not the ones specific to a concrete class like Array List)
  2. use $V{yourArrayListVariableName}.add($F{FieldToAdd}) to add to your collection. 
max_max_mir's picture
Joined: Jul 21 2010 - 9:03am
Last seen: 4 months 3 weeks ago

for some reason, ArrayList was adding values twice, so I ended up using TreeSet

max_max_mir - 5 years 1 month ago

ArrayList adding values twice for me as well.
Any sugestions?

jan.carlsson - 4 years 1 month ago

And TreeSet cuts off all duplicate values, so it is distinct values

jan.carlsson - 4 years 1 month ago
Feedback