velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henning P. Schmiedehausen" <...@intermeta.de>
Subject Re: [FAQ] How do you check for null?
Date Mon, 06 Dec 2004 15:28:16 GMT
Simon Christian <simon@stoutstick.com> writes:

This testing is a job for a programming language. If you really need
checking for "" and null, then write yourself an ultrasmall tool:

public boolean isNull(Object obj)
{
	if (obj == null)
	{
		return null;
	}

	if (obj instanceof String)
	{
		return StringUtils.isEmpty((String) obj);
	}

	return false;
}

#if ($nulltool.isNull($car.fuel))

With the slides from Will's talk @ ApacheCon, you should be able to
add a new statement to the VTL so you can do

#ifnull($car.fuel)

	Regards
		Henning



>Mike Kienenberger wrote:
>> Simon Christian <simon@stoutstick.com> wrote:
>> 
>>>Mike Kienenberger wrote:
>>>
>>>>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.
>> 
>> 
>> That's not true.
>> 
>> car.fuel = "" is not the same as car.fuel = null, yet both pass the 
>> condition.
>> 
>> Unless the template designer knows the underlying type of the java object, 
>> the only safe solution is to check for "(null or false) and (null or 
>> empty-string)" => if true, must be null.
>> 

>Sorry Mike you're absolutely right, I was only considering the 
>non-exhaustive set of cases I usually come across and blinded to the 
>other possibilities.

>Humbly,

>- simon

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

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
   Linux, Java, perl, Solaris -- Consulting, Training, Development

What is more important to you...
   [ ] Product Security
or [ ] Quality of Sales and Marketing Support
              -- actual question from a Microsoft customer survey

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