Question

I'm having a problem getting this calculation down. I can do addition, subtract, etc... but what I'm trying to do is this ((a - b)/b) in Jasper reports.

Here's what I've tried so far

${Budget}.subtract($F{Actual})/$F{Budget}

but when I compile it. It says that I have an expression problem. I'm using Jasper Studio expression editor

Both $F{Budget} and $F{Actual} are text fields and Class java.math.BigDecimal

Here's the stace trace

net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : 
Source text : $F{Budgeted}.subtract($F{Actual}.divide($F{Budgeted}))

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

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

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

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

    Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : 
Source text : $F{Budgeted}.subtract($F{Actual}.divide($F{Budgeted}))

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

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

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

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

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

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

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

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

at         net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFiller.run(BaseFillHandle.java:120)

at java.lang.Thread.run(Unknown Source)

   Caused by: java.lang.ArithmeticException: Division by zero

at java.math.BigDecimal.divide(Unknown Source)

at ProjectSummary_1398114778002_614232.evaluateEstimated(ProjectSummary_1398114778002_614232:405)

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

... 9 more
Was it helpful?

Solution

${Budget}.subtract($F{Actual}) will return a BigDecimal reference, so you cannot use division symbol / because this is only supported for primitives (and their class wrappers when unboxed). So, you should use BigDecimal#divide instead.

${Budget}.subtract($F{Actual}).divide($F{Budget})

From your question edit:

Caused by: java.lang.ArithmeticException: Division by zero

Your $F{Budget} variable should not have a zero value. You could use a ternary operator to solve this problem:

${Budget}.subtract($F{Actual}).divide( ($F{Budget}.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : $F{Budget}) )

From a new comment:

I need if $F{Budget} is zero that calculation field gets assigned zero and a number other wise.

Just modify the usage of ternary operator:

($F{Budget} == null || $F{Budget}.compareTo(BigDecimal.ZERO) == 0)) ? BigDecimal.ZERO : $F{Budget}.subtract($F{Actual}).divide($F{Budget})

OTHER TIPS

Are you missing a pair of ()?

Which will create ambiguity of your expression if no association is specified. (Compiler/Parser)

Look at your stack trace. It has this line.

Caused by: java.lang.ArithmeticException: Division by zero

So $F{Budget} is zero, and it's only right that you should be unable to divide by it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top