How to avoid null values in jasper reports

2019-02-04 07:28发布

I have a field in my jasper report which has a expression value like

$F{address_street1}+" "+$F{address_street2}+ " " +$F{address_state} + " "+$F{address_country}+ " "+$F{address_zip}

My problem is that if any of the fields in here is null I get the null value between other things like

101 Main St****null****ILUnited States12345 

Notice the highlighted null. Is there any way I can avoid that?

I have tried checking the null value for a particular field in it using boolean expression and replacing it with blank, but that doesn't seem to work.

14条回答
三岁会撩人
2楼-- · 2019-02-04 07:47

Try following:

$F{address_street1}.toString() == null ? "" : $F{address_street1}+ " " +
$F{address_street2}.toString() == null ? "" : $F{address_street2}+ " " +
$F{address_state}.toString() == null ? "" : $F{address_state}  + " " +
$F{address_country}.toString() == null ? "" : $F{address_country}+ " " +
$F{address_zip}.toString() == null ? "" : $F{address_zip}

Or Set reports property: When resource missing type : Type Empty

both works in my case.

查看更多
狗以群分
3楼-- · 2019-02-04 07:49
($F{address_street1} + " " + $F{address_street2} + " " + $F{address_state} + " " +
    $F{address_country} + " " + $F{address_zip}).replaceAll("null", "")
查看更多
狗以群分
4楼-- · 2019-02-04 07:50

if your fields are Strings just tick the property on (BLANK WHEN NULL). Otherwise use the ternary operator

if null print something else print the field.

field==null?whatever:field 
field=null?false:true

These both ought to solve the issue.

查看更多
Bombasti
5楼-- · 2019-02-04 07:52

@Vash, Yes that is what I would do except I think you might want to put each expression inside of parentheses so that each expression is independent of the others. Like this:

($F{address_street1} == null ? "" : $F{address_street1}+ " ") +
($F{address_street2} == null ? "" : $F{address_street2}+ " ") +
($F{address_state} == null ? "" : $F{address_state}  + " ") +
($F{address_country} == null ? "" : $F{address_country}+ " ") +
($F{address_zip} == null ? "" : $F{address_zip})
查看更多
Bombasti
6楼-- · 2019-02-04 07:53

To solve this problem first, check field properties Blank when null checkbox in IReport or if it is Jasper jrxml file: <textField isBlankWhenNull="true"> .

Then I found two ways to solve this. Choose one way describes in below.
1. Use expression syntax like this

$F{variableName}.equals( "0" )? "" : $F{variableName}

and you can use code like

$F{address_street1}.equals("0")? "" : $F{address_street1} + " " +  
$F{address_street2}.equals("0")? "" : $F{address_street2} + " " +
$F{address_state}.equals("0")? "" : $F{address_state} + " " +  
$F{address_country}.equals("0")? "" : $F{address_country} + " " +
$F{address_zip}.equals("0")? "" : $F{address_zip}

2. Use expression syntax like

$F{variableName}== null ? "" : $F{variableName}

and you can use code like

$F{address_street1} == null ? "" : $F{address_street1} + " " +  
$F{address_street2} == null ? "" : $F{address_street2} + " " +
$F{address_state} == null ? "" : $F{address_state} + " " +  
$F{address_country} == null ? "" : $F{address_country} + " " +
$F{address_zip} == null ? "" : $F{address_zip}
查看更多
闹够了就滚
7楼-- · 2019-02-04 07:54

i have similar case that i want to avoid showing null in the output if i have null parameter
i use T($P{city}) in Expression editor, it evaluate to empty string if you did not pass parameter to it

T() return the text string if the value is a string, otherwise an empty string is returned

example usage:

$P{name} + " " + (T($P{city}).isEmpty() ? "" : " from " )+ (T($P{city}).isEmpty() ? "" : "\"" +  T($P{city}) + "\"")

if i pass parameter to $P{city}
the result is :

abdulkahliq from "Riyadh"

if i don't pass parameter to $P{city} the result is :

abdulkahliq

Thanks and Regards,

查看更多
登录 后发表回答