开发者

How to avoid null values in jasper reports

开发者 https://www.devze.com 2023-01-05 22:06 出处:网络
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}

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 t开发者_JAVA技巧hings 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.


Set the property isBlankWhenNull to true.

  • In iReport check the Blank When Null checkbox when your field is selected.
  • In jasper jrxml file: <textField isBlankWhenNull="true">


In The Expression You are allowed to use the Java code.

So what You need to do is to check that the value of field is null if is then replace it with empty string.

$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}


@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})


if you can use jasperreports-functions and you want to output String value, you can do it with function T() which returns text String or empty String.

T($F{firstName})


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}


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


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.


I have the option (Blank when Null) checked for every field in the report and still seeing nulls on the fields. So, I used report expressions.

Since every report variable is a string, to check for null use:

$F{address_stree2}.equals("null") 


If you are working in ecliple+jasper softReports you just fallow below steps 1.select field + rightclick and select showProperties option 2.click TextField select BlankWhenNull 3.Compile and Rebuild check it.


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,


You can create a new Style
mark it as Default Style
go to: Advanced > Blank When Null > true
That's it!

All TextField's will 'inherit' this setting
(unless they 'touched' this property. right-click > Reset To Default)


You can write a java Code

 package com.xyz

 Class ReportUtil 
  {
      public static String getMyString (String str1 , String str2)
         {
             if((str1!=null) && (str2!=null))
                 return str1 + " " + str2 ;
             else if str1==null
                  return str2 ; 
             return str1 ;                            
          }

   }

In JRXML , you can use the following expression in the textbox

com.xyx.ReportUtil.getMyString ($F{firstName},$F{lastName}) 

Set "Is blank When null" property to true

Regards,

Ankush


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.


You can set the textfield height as 1, set the Stretch with overflow flag as true and Blank when null as true so when field value is blank it will not leave blank space between.


($F{address_street1} + " " + $F{address_street2} + " " + $F{address_state} + " " +
    $F{address_country} + " " + $F{address_zip}).replaceAll("null", "")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号