Discussion:
[FreeMarker-user] variable?? is not checking for null ... please help
marcusadamski
2012-08-06 22:55:19 UTC
Permalink
Hi all;

A little bit of a newbie with Freemarker, but according to the documentation
the following should work:

<#assign result = mypackage.function()!>
<#if result??>
result.function()
.... do stuff, when result is NOT NULL


where mypackage.function has been passed into the Freemarker template before
processing.

I know the mypackage,function works, and passes back valid objects. However,
when the Java function returns a null, the <#if result??> allows the null to
pass - causing a problem when I try to access result.function()


Can somebody please, explain why the above <#if> doesn't catch a null (and I
know a null is being passed in, due to logging).

Many thanks for any feedback,
Marc




--
View this message in context: http://freemarker.624813.n4.nabble.com/variable-is-not-checking-for-null-please-help-tp4654193.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Denis Bredelet
2012-08-07 09:21:50 UTC
Permalink
Hi Marc
Post by marcusadamski
Hi all;
A little bit of a newbie with Freemarker, but according to the documentation
<#assign result = mypackage.function()!>
What is the "!" for here?

Cheers,
-- Denis.
Post by marcusadamski
<#if result??>
result.function()
.... do stuff, when result is NOT NULL
where mypackage.function has been passed into the Freemarker template before
processing.
I know the mypackage,function works, and passes back valid objects. However,
when the Java function returns a null, the <#if result??> allows the null to
pass - causing a problem when I try to access result.function()
Can somebody please, explain why the above <#if> doesn't catch a null (and I
know a null is being passed in, due to logging).
Many thanks for any feedback,
Daniel Dekany
2012-08-07 10:48:13 UTC
Permalink
Post by marcusadamski
Hi all;
A little bit of a newbie with Freemarker, but according to the documentation
<#assign result = mypackage.function()!>
<#if result??>
The problem is that with the `!` you said that if the return value is
null, then it should be defaulted to empty string + empty sequence +
empty hash (a multi-type value). So after that `result` does exist,
but it will not have a `function` sub-variable.

Of course, I see why the `!` is there. FreeMarker refuses to do
*anything* with null, other than applying `!` and `??` and like on it,
so you can't even store null. Certainly you don't want to call the
function twice either (first to check if it's null, then to store the
non-null result). It's an annoying heritage. The hack to work this
around is using a default that can't be returned by the function. For
example, assuming that you know that the function can't return an
empty Map or empty Collection or empty String:

<#assign result = mypackage.function()!>
<#if result?has_content>
--
Best regards,
Daniel Dekany
marcusadamski
2012-08-07 22:00:16 UTC
Permalink
Many thanks for the feedback. Before I received the responses, I also found
the following worked:

<#assign result = mypackage.function()!>
<#if result.property??>

.. where "property" will be used in the logic after the conditional "if"
Loading...