Jump to content
We've recently updated our Privacy Statement, available here ×

type casting of numeric fields


dprogrammer

Recommended Posts

I don't know why Java is so hard when it comes to type casting. I have few numeric fields of type BigDecimal. When I try to add them together and if one of them is null, the whole total also shows as null. So. I try to evaluate each field before addition like this:

$F{QTY1}.equals(null)?0:$F{QTY1}

The error I get is:

Incompatible conditional operand types int and BigDecimal

I even tried using:

$F{QTY1}.intValue().equals(null)?0:$F{QTY1}

but got error: Cannot invoke equals(null) on the primitive type int

Any ideas to save my frustration?



Post Edited by dreporter at 09/02/2009 15:52
Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

dreporter
Wrote:

I did that. I changed Default Expression language to Groovy. I stil don't see the whole total correctly. It appears as null. If I have to apply casting again even after use of Groovy then what is the advantage of using it?

I said that I didn't think that will allow you to add null BigDecimals.

Groovy would help with the "Incompatibleconditionaloperandtypes" error and in general allow you to write simpler expressions (no intValue() or new Integer(..) required).

Regards,

Lucian

Link to comment
Share on other sites

  • 2 years later...

 Hi,

I am trying to use the tertiary operator technique for a Text Field expression on an Integer parameter and field,
but the result is always null, if the parameter or field is empty.

e.g "$P{integer1} ? new Integer(1) : new Integer(0)"

In a simple report (jrxml is below), using the Empty datasource, I have 2 fields (a String and an Integer)
and 2 parameters  (also a String and an Integer).

When previewed, the String Field and Parameter show "FALSE", but the Integer Field and Parameter shows null.

If the Report language is changed to Java, and the expressions modified to 

"$P{integer1} != null ? new Integer(1) : new Integer(0)"

then the correct values are shown.

Is this actuallly a Groovy issue, or is there something in JasperReports that is causing this ?

Thanks

 

 

 

Code:
<?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">	<property name="ireport.zoom" value="1.5"/>	<property name="ireport.x" value="0"/>	<property name="ireport.y" value="0"/>	<parameter name="integer1" class="java.lang.Integer" isForPrompting="false"/>	<parameter name="string1" class="java.lang.String" isForPrompting="false"/>	<field name="field1" class="java.lang.String"/>	<field name="field2" class="java.math.BigDecimal"/>	<title>		<band height="56" splitType="Stretch">			<staticText>				<reportElement x="0" y="21" width="555" height="20"/>				<textElement textAlignment="Center">					<font size="14"/>				</textElement>				<text><![CDATA[Test Report for null Parameters and Fields]]></text>			</staticText>		</band>	</title>	<detail>		<band height="132" splitType="Stretch">			<textField>				<reportElement x="17" y="14" width="419" height="20"/>				<textElement>					<font size="14"/>				</textElement>				<textFieldExpression class="java.lang.String"><![CDATA[$F{field1} ? "TRUE" : "FALSE"]]></textFieldExpression>			</textField>			<textField>				<reportElement x="17" y="92" width="419" height="20"/>				<textElement>					<font size="14"/>				</textElement>				<textFieldExpression class="java.lang.Integer"><![CDATA[$P{integer1} ? new Integer(1) : new Integer(0)]]></textFieldExpression>			</textField>			<textField>				<reportElement x="17" y="72" width="419" height="20"/>				<textElement>					<font size="14"/>				</textElement>				<textFieldExpression class="java.lang.String"><![CDATA[$P{string1} ? "TRUE" : "FALSE"]]></textFieldExpression>			</textField>			<textField>				<reportElement x="17" y="32" width="419" height="20"/>				<textElement>					<font size="14"/>				</textElement>				<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{field2}  ? new Integer(1) : new Integer(0)]]></textFieldExpression>			</textField>		</band>	</detail></jasperReport>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...