Velocity nulls and empty strings

You want Quiet Reference Notation: $!a

Here's your example:

This is the variable $!a.

If $a is null or "", Velocity will render:

This is the variable .

Official Guide section: https://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation


Another alternative is to modify your if statement per Checking for Null (thanks for the link @xavi-lópez):

Approach 2: Use the fact that null is evaluated as an empty string in quiet references. (cf. http://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation)

So, your code would be:

#set ( $a = "")
#if ("$a" != "") 
   assert("never prints a neither gets here: " + $a)
#end

$!a does the trick. You can use this form directly without an if check.