velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Christian <si...@stoutstick.com>
Subject Re: [FAQ] How do you check for null?
Date Fri, 03 Dec 2004 18:07:50 GMT
Mike Kienenberger wrote:
> Shinobu Kawai <shinobu.kawai@gmail.com> wrote:
> 
>>Problem: I want to check for null, something like this:
>>    #if ($car.fuel == null)
>>
>>
>>Approach 1: Use the fact that null is evaluated as a false conditional.
>>  cf. http://jakarta.apache.org/velocity/user-guide.html#Conditionals
>>    #if( ! $car.fuel )
>>
>>Note : The conditional will also pass if the result of $car.fuel is
>>the boolean false.  What this approach is actually checking is whether the
>>reference is null or false.
>>
>>
>>Approach 2: Use the fact that null is quiet references.
>>  cf. 
> 
> http://jakarta.apache.org/velocity/user-guide.html#Quiet%20Reference%20Notation
> 
>>    #if( "$!car.fuel" == "" )
>>
>>Note : The conditional will also pass if the result of $car.fuel is a
>>blank String.  What this approach is actually checking is whether the 
> 
> reference
> 
>>is null or blank.
>>BTW, just checking for blank can be achieved by:
>>    #if( "$car.fuel" == "" )
> 
> 
> So you really either need to use the tool or do this:
> 
> #if ((! $car.fuel) && ("$!car.fuel" == ""))
> 
> -Mike
> 

No the first bit isn't necessary, just #if( "$!car.fuel" == "" ) covers 
the eventualities when testing for null. I guess if there was a 
possibility that the variable could return null or false, and you needed 
to know the different then you might need to do:

  #if( $car.fuel )
   keep on rolling
  #elseif( ! $car.fuel && "$car.fuel" != "" )
   better stop real soon now
  #else
   fuel gauge must be broken
  #end

However I'd hazard that you're better off adding a method that tests the 
'false and not null' case e.g. isFuelGaugeBroken(). (Your method name 
may change dependent on the exact application...)

- simon





---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message