Evaluation of Expression Error in If-Condition

0

Hi, currently I’m trying to fill the captions of up to eight table columns depending on a list of entries I get as a parameter. The list can be smaller than eight, but never bigger, so in my opinion the following code should work in the field expression.

IF($P{List}.size()<2,"No Entry",$P{List}.get(1))

As an example for the second column.

The subreport in question compiles without any error, but the main report crashes when I try to fill it. There surely is something that I'm doing wrong, but I even tried using that expression in a variable and print the variables content in the field, same result, same error.

I get the feeling Jasper evaluates the whole statement, sees "Oh, I can't get into the else case because the list is to small" and crashes, even though it could perfectly use the if-case. Is there a workaround or a real solution to this problem?

 

 

net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: IF($P{List}.size()<2,"No Entry",$P{List}.get(1))

                at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:524)

                at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$20(ReportControler.java:499)

                at com.jaspersoft.studio.editor.preview.view.control.ReportControler$5.run(ReportControler.java:380)

                at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

Caused by: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: IF($P{List}.size()<2,"No Entry",$P{List}.get(1))

                at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:886)

                at net.sf.jasperreports.components.table.fill.FillTableSubreport.prepareSubreport(FillTableSubreport.java:172)

                at net.sf.jasperreports.components.table.fill.FillTable.prepare(FillTable.java:419)

                at net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:150)

                at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:352)

                at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:421)

                at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:396)

                at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillBandNoOverflow(JRVerticalFiller.java:432)

                at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillPageHeader(JRVerticalFiller.java:392)

                at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:253)

                at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:119)

                at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:558)

                at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:396)

                at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:742)

                at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:58)

                at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:216)

                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

                at java.lang.Thread.run(Thread.java:745)

Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: IF($P{List}.size()<2,"No Entry",$P{List}.get(1))

                at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(JREvaluator.java:357)

                at net.sf.jasperreports.engine.fill.JRCalculator.evaluateEstimated(JRCalculator.java:609)

                at net.sf.jasperreports.engine.fill.JRCalculator.estimateVariables(JRCalculator.java:208)

                at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1276)

                at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1250)

                at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1010)

                at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:117)

                ... 8 more

Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

                at java.util.ArrayList.rangeCheck(ArrayList.java:653)

                at java.util.ArrayList.get(ArrayList.java:429)

                at PersonalData_DummyDataset_1457336155313_576402.evaluateEstimated(PersonalData_DummyDataset_1457336155313_576402:398)

                at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(JREvaluator.java:343)

                ... 14 more

MiditecReportDev's picture
Joined: Mar 6 2016 - 11:42pm
Last seen: 1 year 1 month ago

3 Answers:

1

Try the following format;

(condition) ? true : false 

Hope this helps,
KKriplani

kkriplani's picture
858
Joined: Sep 4 2015 - 2:18am
Last seen: 3 weeks 4 days ago
0

It's because Java array index starts from 0. Try .get(0)

hozawa's picture
52632
Joined: Apr 24 2010 - 4:31pm
Last seen: 5 months 1 week ago
0

@hozawa

I know that much, that's why I check if the size it at least two before selecting index 1 - because size1=index0 available and size2=index1 available. But that's not working ;)

 

@kkriplani

Will try your solution now. You want me to paste that in the Expression-Field, just to clarify this?^^

MiditecReportDev's picture
Joined: Mar 6 2016 - 11:42pm
Last seen: 1 year 1 month ago

This occurs because in this case $P{List}.get(1) is a function argument, and it gets always evaluated, even when the list size is 1.
(condition) ? true : false does not have this problem

shertage - 3 years 8 months ago

Just tried it out - works perfectly. Thanks @ shertage & kkriplani. :)

MiditecReportDev - 3 years 8 months ago
Feedback