1
votes

I'm trying to build an HTML table from a collection of Iterable<Object> instances.

final Iterable<Iterable<Object>> rows = Lists.newArrayList(
    Lists.newArrayList( 1, "Fizz"),
    Lists.newArrayList( 2, "Buzz"),
    Lists.newArrayList( 3, null ));

This is added to my model with a key of "rows".

In my template file, I have the following markup:

<#list rows as row>
  <tr>
    <#list row as value>
      <td>${value!}</td>
    </#list>
  </tr>
</#list>

When I try to print out these values, the template falls over handling the null value of the final row:

freemarker.core._TemplateModelException: The FreeMarker value exists, but has nothing inside it; the TemplateModel object (class: freemarker.ext.beans.StringModel) has returned a null instead of a String. This is possibly a bug in the non-FreeMarker code that builds the data-model.

The blamed expression:
==> value!

I have tried replacing ${value!} with ${value?has_content} and tried a few other combinations <#if value??> around the block, but I keep getting the same error.

How can I make my Freemarker template accept these null values and produce an empty string?

3
Look here is the answer: Handling null values in Freemarker - ACV
As the error message suggests, it has to be figured out where do those StringModel-s with a null inside come from. The template language can't handle this because it's should happen. - ddekany

3 Answers

0
votes

Most likely your value object returns null form it's toString() method.

0
votes

This should work:

${value!""}
-1
votes

You can use the ?? test operator:

This checks if the attribute of the object is not null:

<#if object.attribute??>

This checks if object or attribute is not null:

<#if (object.attribute)??>

Source: FreeMarker Manual

Look here is the answer: Handling null values in Freemarker