2

I discovered today in iReport that I cannot set the initial value expression of a String[] using normally valid Java syntax, such as:

private String[] fruitNames = new String[] {"Apple", "Banana"};

Extrapolating this into an iReport variable would be something as simple as this (I would think):

Name: fruitNames

VariableClass: java.lang.String[]

Calculation: Nothing

Reset Type: Report

IncrementType: None

Initial Value Expression: new String[]{"Apple", "Banana"}

Compiling my report template I get an error:

Compilation exceptions: com.jaspersoft.ireport.designer.compiler.ErrorsCollector@683896bd >net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions c>lass file: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: >calculator_Fruits_1326149102402_537017: 281: unexpected token: Apple @ line 281, column 55. >1 error

In the code editor for setting the initial value I notice that the parser underlines, in red, the curly braces {}. This makes sense because iReport interprets these braces as param, field, or variable identifiers. So I see where the conflict can exist, but does anyone know the proper syntax to use for initializing String[] in the Initial Value Expression field?

Note: I got around the issue by just setting the value in a Scriptlet, but I'm really curious to know the proper syntax, if it exists.

Alex K
  • 22,315
  • 19
  • 108
  • 236
depth13
  • 23
  • 1
  • 2
  • 5
  • 1
    You can change report's language to `Java` (perhaps it is Groovy now). You can try to initialize the array with help of `String.split(String delimiter)` method – Alex K Jan 10 '12 at 20:02
  • just wondering, what is the reason of using the array of String? – Alex K Jan 10 '12 at 20:06

2 Answers2

2

You should be able to get what you need by setting your variable class to java.util.Collection. Then set the initial value like this:

java.util.Arrays.asList( "Apple", "Banana" )

Also, your error indicates that your report language is set to Groovy. That's fine if it's intentional, but perhaps it's accidental. I find things are simplified by changing the report language to Java.

I don't normally set the variable class to an array like that. I set it to java.util.Collection as mentioned above. But I don't know if there is any important difference.

mdahlman
  • 9,204
  • 4
  • 44
  • 72
  • Your solution for the initial variable expression works, and therefore I will mark your reply as the answer. Thank You Very much! But it is not true that you cannot set the variable class as an array, such as String[]. You definitely can, and in fact I am doing so. Although maybe that is not the correct way. But it does work. – depth13 Jan 10 '12 at 19:19
  • Hmm... In that case I'll update my answer to remove the false information. – mdahlman Jan 10 '12 at 20:03
0

I just ran into the same issue. The real problem is that you are using a Java expression, but the iReports tool is set to the language of Groovy, hence the Groovy expression. The previous user mentioned that. If you went into the properties, find the Language line, and change that to Java. That worked for me. The previous user is correct, but just wanted to point out that the real issue I believe is your report set to Groovy.