J. Kamans J. Kamans - 3 years ago 134
Java Question

Freemarker error when ?join on null field since changing SpringBoot dependency

Solved: the error was already there, I only assumed I recently created it...

I have a project where I'm using SpringMVC and Freemarker.

In my controller I add an attribute to the model named
like this:

public String activities(Model model, @PathVariable String floorId) {
model.addAttribute("activities", activityService.getActivitiesForFloor(floorId));
return "floor/activities";

has a field called
which sometimes is
. This is perfectly fine.

In my Freemarker view I use the modelattribute like this:

<#list activities as activity>
<td>${activity.involvedTiles?join(", ")}</td>

This has always worked (it never gave an error). But now I suddenly get this error:

2016-10-21 13:56:17,163 ERROR [http-nio-8080-exec-10] o.a.c.c.C.[.[.[.[dispatcherServlet] [DirectJDKLog.java:181] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> activity.involvedTiles [in template "floor/activities.ftl" at line 51, column 27]

Tip: It's the step after the last dot that caused this error, not those before it.
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present#deleted-channelwhen-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??

I understand I'm getting this error, because
and I try to use it with the
function. But I'm not understanding why this suddenly stopped working. The only thing I can think of is that I've updated my SpringBoot dependency from 1.3.0.RELEASE to 1.4.1.RELEASE. In SpringBoot 1.3.0 they use org.freemarker 2.3.23 and in SpringBoot 1.4.1 they use org.freemarker 2.3.25-incubating.

My question: did Freemarker change something and is that why I'm getting this error?

I checked the site of freemarker 2.3.23 and freemarker 2.3.25 but couldn't find something that explains this error.

Answer Source

?join was never intended to tolerate a null, and there was no such bug (accidentally allowing null) either as far as I know. Also, I have tried this with FreeMarker 2.3.21 (a really old version) and it still doesn't allow null there.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download