$V{MyVariable} == null -> returns null

1

Hello guys,

Well, almost all is said in the title.

I currently have a strange behavior working with iReport ( / jasper reports) 4.0.2. : for some fiels and variables, i try to replace null values with fixed values (most of the time "0"). To do this simple thing, i use a small piece of code in the variable expression :

($F{MyField} == null) ? new Double("0") : $F{MyField}

But i still get null values in the report. Before getting completely nuts, i made a new test with a new boolean variable :

$F{MyField} == null

and, surprise, it returns null. Ok now I understand why this is not working for my variables... but why is this condition not giving true when myField is null ?

 

By the way, "Replace null by blank" option is working, but is not sufficient for my report.

 

Does anyone have an idea ?

 

Regards

Banania

banania's picture
11
Joined: Jun 29 2011 - 12:32pm
Last seen: 8 months 2 weeks ago

8 Answers:

0

Hello,

Nobody has a clue ?

Can it be some kink of bug ? Do I make something definitivly wrong ?

 

 

Regards

Banania

banania's picture
11
Joined: Jun 29 2011 - 12:32pm
Last seen: 8 months 2 weeks ago
0

Are you sure it is not "null" string value as compared to null? 

Tick the 'Blank when null' option in ireports to see if that works.

adigar's picture
7
Joined: Jun 9 2011 - 6:03pm
Last seen: 7 years 11 months ago
0

Thanks for your answer.

 

Yes good point, but checked the database and it's really a NULL value. If I check "blank on null" value is blanked.

 

I made a really simple report just to reproduce problem. See below.

Variable "variable1" just check if MT_LIB field is null. It prints 'false' is field is not null... but it print "null" instead of "true" if field is null.

I'm losing my last hairs...

 

<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[SELECT CO_NUMCO, LI_MTLIB
FROM table1
WHERE field1 = 69906]]>
    </queryString>
    <field name="CO_NUMCO" class="java.lang.Integer"/>
    <field name="LI_MTLIB" class="java.lang.Double"/>
    <variable name="variable1" class="java.lang.String">
        <variableExpression><![CDATA[$F{LI_MTLIB}==null]]></variableExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="24" splitType="Stretch">
            <staticText>
                <reportElement x="36" y="2" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[CO_NUMCO]]></text>
            </staticText>
            <staticText>
                <reportElement x="189" y="2" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[LI_MTLIB]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <textField>
                <reportElement x="19" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.Integer"><![CDATA[$F{CO_NUMCO}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="150" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.Double"><![CDATA[$F{LI_MTLIB}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="289" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[$V{variable1}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>
 

banania's picture
11
Joined: Jun 29 2011 - 12:32pm
Last seen: 8 months 2 weeks ago
0

Dear Bana..

 

As per my understanding, you have a field where there is some data on database and another consist of null value.

and you want to print '0' in place of null .....if i m on right path then

 

solution is here...............!

you already had a field associated with database field....now you drawn on static label exactly above the field where you want to print '0' in case value is null then make it hide by uncheck on opaque so that it should not display normally.....!....now use print when expression properties of jasper on that field and put condition like new Boolean(field name(having value from database =="" or having value from database==null) and on acutal field use print on expression with condition new Boolean(field name having value from database!=null)

Hope this will work!......

anilkumarpoddar's picture
Joined: May 7 2011 - 12:06am
Last seen: 8 years 2 weeks ago
0

This won't address the underlying issue with your report design but will give you the results you want: force the database to always return a value in that field.

eg. SELECT field1, field2, nvl(problemfield,0) as problemfield FROM table.....

Note: The equivalent to Oracle's nvl() in MySQL is ifnull(), in postgres and db2 it's coalesce(), in MS-SQL it's isnull()

jmurray's picture
549
Joined: Dec 11 2006 - 11:19am
Last seen: 12 years 5 months ago
1

 Hi,

One point, are you using "Groovy" for the Report Language ?

If so, try changing it to "Java".

I have had similar problems trying to handle null Integer parameters; in Groovy, you can just say

$F(field1) ? $F(field1) : "Default Value"

If something is null its effectively  FALSE.

However, I have only manage to get this to work with Strings.

Steve

stevep0000's picture
Joined: Sep 30 2010 - 6:56am
Last seen: 5 months 3 weeks ago

I have wriiten this code.
($F{FirstColorName}=="NONE") ? "" : $F{FirstColorName}
The output of this code is None, noun, etc, and that first color name. Now i want to Show the coloumn will be blank instead of that none. Help me..

ganapathy_1 - 6 years 3 months ago
0

It's 3 years later and I'm sure the original poster does not remember the solution, but I'm having the exact same problem.

I have a variable that has the result of a scriplet method which returns "null". It's not a string, and it doesn't seem like an object eitherbecause I can't compare it to anything.. It is just null, plain and simple.

How do I get around this problem? I need to know if it is null to be able to "ignore" it's value.

This is what I wanted:

($V{IMAGE_PATH} != null ) ? $V{IMAGE_PATH} : "" 

Would greatly appreciate some input =)

Cheers from Spain!

l_claudia90's picture
Joined: Aug 14 2014 - 4:47am
Last seen: 2 years 8 months ago

Well, since I didn't get a reply, I did a workaround especific to my own code that didn't evaluate the null expression.

Thanks anyway!

l_claudia90 - 4 years 9 months ago
0

Hi What I did is the following: In the sql statement I'm working with postgres check syntax to another database. 

I added :

 select field1,field2, case when (contract.date_end IS NULL) THEN 0 ELSE 1 END as check,

So I have a field called check

And I used in an expression as an IF

$F{check}==0 ? " A text according to the definition" : " to " + $F{fechaf}.toString()

I hope this will help someone. 

The other solution could be create a class and added to the report as a jar file. 

 

jsuarezc1's picture
Joined: Nov 29 2017 - 4:16pm
Last seen: 3 months 3 weeks ago
Feedback
randomness