How to avoid null values in jasper reports

2019-02-04 07:39发布

问题:

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.

回答1:

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">


回答2:

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}


回答3:

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


回答4:

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



回答5:

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}


回答6:

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


回答7:

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") 


回答8:

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



回答9:

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.



回答10:

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,



回答11:

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.



回答12:

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.



回答13:

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.



回答14:

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