1

Currently, I am working in JasperReport Server and iReport. The version of JasperReport 5.5.0 and iReport are 5.5.0. And the database is MYSQL.

I declared the variable Dr like the following where am_primeamt is coming from SQL query.

<variable name="Dr" class="java.lang.Double">
    <variableExpression><![CDATA[($F{am_primeamt} > 0 ? $F{am_primeamt} : 0 )]]></variableExpression>
</variable>

And tried to show like following:

<textField pattern="#,##0.00">
<reportElement x="427" y="0" width="82" height="20" uuid="332ceda3-5237-40b5-a0ef-3aad009a7911">
    <printWhenExpression><![CDATA[$V{Dr} != 0]]></printWhenExpression>
</reportElement>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{Dr}]]></textFieldExpression>

When I am trying to see the preview it's showing the following error:

Error filling print... Error evaluating expression :      Source text : $V{Dr} != 0 net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :      Source text : $V{Dr} != 0      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:244)      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:591)      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:559)      at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:1016)      at net.sf.jasperreports.engine.fill.JRFillElement.evaluatePrintWhenExpression(JRFillElement.java:795)      at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:482)      at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:259)      at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:456)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2057)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:778)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:288)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:151)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:932)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)      at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:87)      at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)      at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:276)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:745)      at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:891)      at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)      at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)  Caused by: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Double      at gl_transaction_1454989470091_439976.evaluate(gl_transaction_1454989470091_439976:363)      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:231)      ... 20 more  Print not filled. Try to use an EmptyDataSource...

Edited: Language is set java from the beginning and that showed the error like this. After making it groovy, it solved my problem.

2
  • @PetterFriberg That post did the opposite to my problem as it solved after making it groovy. Commented Feb 9, 2016 at 11:36
  • Hmm,your error is java.math.BigDecimal cannot be cast to java.lang.Double and your solution is changing to groovy?, well ok...., please add the jrxml of $F{am_primeamt} to create a complete minimal reproducible example, since probably you have class="java.math.BigDecimal" in it... Commented Feb 9, 2016 at 16:20

1 Answer 1

0

The accepted answer does not address the problem and since its upvoted with deleted users (ask your-self why) even if question does not have complete mcve I have decided to answer to help future viewer to understand the issue. The accepted answer has been deleted by moderators, but question remains and has indicated the solution of setting language groovy

The issues is:

java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Double

This is most probably caused by that the field $F{am_primeamt} is defined as java.math.BigDecimal

<field name="am_primeamt" class="java.math.BigDecimal">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>

The correct solution would be to change the current variable declaration to same class or return declared class

Same class

<variable name="Dr" class="java.math.BigDecimal">
   <variableExpression><![CDATA[($F{am_primeamt}.doubleValue() > 0 ? $F{am_primeamt} : new java.math.BigDecimal(0) )]]></variableExpression>
</variable>

Note: printWhenExpression not to be changed to example $V{Dr}.doubleValue() != 0

Declared class

<variable name="Dr" class="java.lang.Double">
   <variableExpression><![CDATA[($F{am_primeamt}.doubleValue() > 0 ? $F{am_primeamt}.doubleValue() : 0d )]]></variableExpression>
</variable>

Why does groovy works?, it does automatic conversion using java.lang.Number and use operator overloading to allow direct mathematical operations on the BigDecimal object see http://www.groovy-lang.org/, but before you choose it instead of java its better to understand why.......

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.