variable?? is not checking for null ... please help

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

variable?? is not checking for null ... please help

marcusadamski
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
Reply | Threaded
Open this post in threaded view
|

Re: variable?? is not checking for null ... please help

Denis Bredelet
Hi Marc

> Hi all;
>
> A little bit of a newbie with Freemarker, but according to the documentation
> the following should work:
>
> <#assign result = mypackage.function()!>

What is the "!" for here?

Cheers,
-- Denis.

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


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: variable?? is not checking for null ... please help

Daniel Dekany
In reply to this post by marcusadamski
Tuesday, August 7, 2012, 12:55:19 AM, marcusadamski wrote:

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

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


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: variable?? is not checking for null ... please help

marcusadamski
In reply to this post by marcusadamski

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"

From your feedback, I now understand why the "<#if result??> " doesn't work, as "result" at this stage is an empty string...

Many thanks for your speedy help!
Marc