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.
6 Answers:
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 ! !
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?
I have encountered a situation just like what you did.
Here are a few things you can do
- 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)
- use $V{yourArrayListVariableName}.add($F{FieldToAdd}) to add to your collection.
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.
This worked for me and is a HUGE HELP.