Charlie Zeng
Charlie Zeng

Reputation: 131

Can't print data from 2 layer ArrayList using Freemarker

I'm using freemarker 2.3.23 in eclipse to generate reports. Below are the code for Data-model:

ArrayList<Cell> namelist=new ArrayList<Cell>();         
Cell cell1=new Cell();      
cell1.data.add("element1");         
namelist.add(cell1);
Cell cell2=new Cell();
cell2.data.add("element2");
namelist.add(cell2);
data.put("namelist", namelist);

Code for class Cell:

public class Cell {
    public ArrayList<String> data;
    public Cell(){
        data=new ArrayList<String>();
    }
}

Code for template:

  <#list namelist as name>
    <#list name.data as element>
      ${element}
    </#list>
  </#list>

But got error stack as below:

FreeMarker template error: The following has evaluated to null or missing: ==> name.data [in template "report.ftl" at line 33, column 16]

---- 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<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression,

use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??

---- FTL stack trace ("~" means nesting-related):

- Failed at: #list name.data as element [in template "report.ftl" at line 33, column 9]

Upvotes: 0

Views: 809

Answers (1)

Sleafar
Sleafar

Reputation: 1526

It seems you have to create a getter for data. Form the docs:

Note that public fields are not visible directly; you must write a getter method for them.

Upvotes: 1

Related Questions