ClassCastException when evaluate DB Number DType

Hi,

 

I am using Jasper Report with Hibernate and Spring env. Everything seems to work great excepts that when my hibernate query has a field of NUMBER type (Oracle), the report throws ClassCastException when evaluates the JRField.

 

Here is brief of what I have:

1. Query in the hibernate mapping file(hbm.xml file):

i.e SELECT a, b, c from myTable; (where a and c are VARCHAR2 and b is NUMBER. I named mySelect1).

2. Created a Class (myHQDS) to implement JRDataSource interface.

3. Code in the program:

List report = session.find("mySelect1");
 
Map parameters = new HashMap();
parameters.put("Title", "The Report");
 
...Load and compile the report
 
String[] fields = new String[] { "a", "b", "c"};
myHQDS ds = new myHQDS(report, fields);
JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, parameters, ds);
 
JasperManager.printReportToPdfFile(jasperPrint, "the-report.pdf");

4. Error encountered:

net.sf.jasperreports.engine.fill.JRExpressionEvaluationException: Error evaluation exception

Source text: $F{b}.

...

java.lang.ClassCastException: java.math.BigDecimal

at the-report_12.....evaluate(the-report_12......:201)

net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java: 186)

I changed the filed name in the mapping file as well as the java object from int, Integer to java.lang.Number ...etc, but still I have the error when evaluate this filed. If I commeded this field out, it works perfectly fine. Can some body help???

Thanks in advance,

Bob

bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago

14 Answers:

Does anyone have any answer?
bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago

Post your JRXML.

If you're using $F{b} as expression for a text field, make sure you do <textFieldExpression class="java.lang.Number">$F{b}</textFieldExpression>

Regards,

Lucian

lucianc's picture
87271
Joined: Jul 17 2006 - 1:10am
Last seen: 12 hours 36 sec ago

Lucian,

Thanks for your response. Yes I tried to change the data type from Number, int to BigDecimal...etc. But I still the ClassCastException error when evaluate field. Below is the simplyfied version of my JRXML. Thanks-

 

<?xml version="1.0"?>

<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="myTemplate">

<parameter name="TITLE" class="java.lang.String"/>

<field name="a" class="java.lang.String"/>
<field name="b" class="java.lang.Number"/>
<field name="c" class="java.lang.String"/>


<detail>
   <band height="40">
     <textField>
       <reportElement x="0" y="0" width="150" height="40"/>
       <textFieldExpression class="java.lang.String">
        <![CDATA[$F{a}]]>
       </textFieldExpression>
     </textField>

     <textField>
       <reportElement x="150" y="0" width="150" height="40"/>
       <textFieldExpression class="java.lang.Number">
        <![CDATA[$F{b}]]>
       </textFieldExpression>
     </textField>

     <textField>
       <reportElement x="300" y="0" width="150" height="40"/>
       <textFieldExpression class="java.lang.String">
        <![CDATA[$F{c}]]>
       </textFieldExpression>
     </textField>
   </band>
</detail>
</jasperReport>

bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago

The JRXML looks fine, assuming that the data source returns a BigDecimal as value for the "b" field.

Create a text field that shows $F{b}.getClass().getName() and see what the output is when you run the report.

Regards,

Lucian

lucianc's picture
87271
Joined: Jul 17 2006 - 1:10am
Last seen: 12 hours 36 sec ago
Hi Lucian,

How can I create a text field $F{b}.getClass().getName()? you mean in JRXML file?
Because, as soon as I include the b field in my hibernate query and in my code, I get the error as I posted above.

Thanks,
Bob
bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago

Where is this error coming from?  Please post the full exception stacktrace.

lucianc's picture
87271
Joined: Jul 17 2006 - 1:10am
Last seen: 12 hours 36 sec ago
Hi Lucian,

My Jasper works is on a diff network so I can't cut and paste it here, but something like this:

net.sf.jasperreports.engine.fill.JRExpressionEvaluationException: Error evaluation exception
Source text: $F{b}.
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java: 197)
net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java: 574)
net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java: 542)
...
...
net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java: 368)
Truncated. See log file for complete Starcktrace
java.lang.ClassCastException: java.math.BigDecimal
at the-report_12.....evaluate(the-report_12......:201)
net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java: 186)
...
net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java: 835)
Truncated. See log file for complete Starcktrace


I ran a debugger and found the values for the 3 fields a, b, and c. The a and c fields values are as normal as String results, but the b field is returned as BigDecimal. Is it normal?

Thanks again for your help.
Bob
bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago
Lucian,
I ran the debug and got the value for "b" field is BigDecimal. I am expecting a numeric value instead...
Do you have idea, suggestion?

Thanks,
Bob
bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago

Could you set the net.sf.jasperreports.compiler.keep.java.file property to true, and check the Java file generated by JR for the report?  Go to line 201 to see how the expression got translated in the file..

Regards,

Lucian

lucianc's picture
87271
Joined: Jul 17 2006 - 1:10am
Last seen: 12 hours 36 sec ago
Hi Lucian,

I made the change and re-run...But don't know where to look for the generated Java file...Where should it be?

Thanks,
Bob
bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago
Can some body help??? I am stuck...
bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago

Sorry for the long pause.

You can tell the compiler where to keep the generated file via the net.sf.jasperreports.compiler.temp.dir property.

If seeing the generated Java file doesn't yield any clues, the only solution is to prepare a self-contained test case to reproduce the problem.  Otherwise I'm afraid this investigation isn't going to have any finalization.

Regards,

Lucian

lucianc's picture
87271
Joined: Jul 17 2006 - 1:10am
Last seen: 12 hours 36 sec ago
Hi Lucian,
I am using Jaspperreports-3.1.0 and couldn't find the property file in net.sf.jasperreports.compiler.temp.dir directory.

Thanks,
Bob
bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago
Lucian,

I found a work around for this issue by adding a check for the returned column in question and convert it to Integer...

i.e:
====
public Object getFieldValue(JRField field) throws JRException{
Object value = null;
int index = getFieldIndex(field.getName());
if (index !=-1){
//This block is added...
Object[] values = (Object[])currentValue;
if("columnInQuestion".equalsIgnoreCase(field.getName()))
{
Integer myInt = new Integer(values[index].toString());
value = myInt;
}else{
value = values[index];
}
}
return value;
}
bobz28's picture
246
Joined: Oct 16 2009 - 12:08pm
Last seen: 13 years 11 months ago
Feedback