Using Freemarker to display a table of arbitrary Java objects and their fields
<p>First, I've read <a href="https://stackoverflow.com/questions/2733446/use-a-single-freemarker-template-to-display-tables-of-arbitrary-pojos">this question</a>, but it didn't resolve my issue.</p>

<p>I am trying to create a table that will display an arbitrary list of Java objects. When I say "arbitrary" I mean both that the amount of objects is arbitrary and that the type of objects is arbitrary (they are all going to be instances of the same class though). I want the rows of this table to represent objects, and the columns to represent the value of each object's instance variable (spreadsheet style, basically). The first row, however, will just be a list of instance variable names.</p>

<p>The objects I am currently testing this on has all variables set to private, but I have provided the relevant getters and setters.</p>

<p>Here is a snippet from my Java code. I am pulling objects from an Oracle Coherence cache, and putting them into an ArrayList. Then I make a string array of the instance variable names.:</p>

<pre><code>        /**
     * Get objects in cache and add to ArrayList.
     */

    for(Iterator iter = currentCache.entrySet().iterator();iter.hasNext();){
        Map.Entry entry = (Map.Entry)iter.next();
        String key = (String) entry.getKey();
        Pof tempPof = (Pof)entry.getValue();
        tableList.add(tempPof);
        System.out.println("one loop");
    }

    request.setAttribute("beans",tableList);

    System.out.println("Size of tableList is: " + tableList.size());
    /**
     * Build an array containing the variable names of cached objects.
     */

    Field[] fields = Pof.class.getDeclaredFields();
    String[] variableNames = new String[fields.length];

    for(int j = 0; j &lt; fields.length;j++){
        variableNames[j] = fields[j].getName();
        System.out.println(variableNames[j]);
    }

    request.setAttribute("colNames",variableNames);


    /**
     * numCols determines the number of columns displayed in the table.
     */

    int numCols = fields.length;
    String[] fieldStrings = new String[numCols];
    request.setAttribute("numCols",numCols);
    Pof thing = (Pof) tableList.get(0);
</code></pre>

<p>Here is a snippet from the relevant .ftl file:</p>

<pre><code>&lt;table border = "1px"&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th colspan="${numCols}"&gt;${selectedCache}&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;#list colNames as colName&gt;
                    &lt;td&gt;${colName}&lt;/td&gt;
                &lt;/#list&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
            &lt;#list beans as bean&gt;
                &lt;tr&gt;
                    &lt;#list colNames as colName&gt;
                        &lt;td&gt;${bean[colName]}&lt;/td&gt;
                    &lt;/#list&gt;
                &lt;/tr&gt;
            &lt;/#list&gt;
        &lt;/tbody&gt;

    &lt;/table&gt;
</code></pre>

<p>This gets me the following error:</p>

<hr>

<p>freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> bean[colName]  [in template "front.ftl" at line 46, column 35]</p>

<p>Tip: It's the final [] step that caused this error, not those before it.</p>

<p>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 &lt;#if myOptionalVar??>when-present&lt;#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: ${bean[colName]}  [in template "front.ftl" at line 46, column 33]</p>

<pre><code>at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134)
at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:451)
at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:374)
at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:96)
at freemarker.core.DollarVariable.accept(DollarVariable.java:59)
Truncated. see log file for complete stacktrace
</code></pre>

<hr>

<p>The problem seems to be my ftl syntax; that is, it doesn't like the expression ${bean[colName]}.</p>

<p>Questions:</p>

<p>1) Is the syntax wrong?</p>

<p>2) Is this something that Freemarker can't do?</p>

<p>3) Should I try another approach? For example, should I just create an array with each bucket containing an array (or another data structure) of the instance variable values?</p>