I have an application with two entities that have a bidirectional one to many relationship. Owners and Bicycles.
So getting the owners via curl will give
[
{"id":1,
"userName":"user1",
"bicycles":
[
{
"id":1,
"make":"dawes",
"model":"civic",
"owner":1
}
]
},
{"id":2,
"userName":"user2",
"bicycles":
[
{
"id":2,
"make":"whyte",
"model":"montpellier",
"owner":2
}
,{
"id":4,
"make":"dahon",
"model":"tern A7",
"owner":2
}
]
} ]
which is fine.
If I create a template which loops in a table,
<table>
<tr th:each="owner : ${owners}">
<td th:text="${owner.userName}"></td>
<td th:text="${owner.bicycles[0].make}"
th:if="${#lists.size(owner.bicycles)} > 0">"</td>
<td th:text="${owner.bicycles[0].model}"
th:if="${#lists.size(owner.bicycles)} > 0"></td>
</tr>
</table>
then I get the expected result in the browser. I realise the above is awful code but I am just interested in getting thymeleaf to work at the moment.
But if I do the following code
<table>
<tr th:each="owner : ${owners}">
<td th:text="${owner.userName}"></td>
<tr th:each="bike : ${owner.bicycles}">
<td th:text="${bike.make}"></td>
<td th:text="${bike.model}"></td>
</tr>
</tr>
</table>
then I get the following console error
nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "owner.bicycles" (template: "nutsthymeleaf" - line 23, col 15)] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'bicycles' cannot be found on null
What I find confusing is that owner.bicycle[index] works. It shows make and model. Yet owner.bicycles appears to be a null field according to the error.
So obviously I am doing something wrong ….
Property or field 'bicycles' cannot be found on nullexception. Just tried it with an example. Check if bicycles is really not null.bicyclesdoesn't exist, but rather that${owner}is null, and so it can't get a property of a null object. Are you sure that you didn't accidentally close the loop and${owner}is out of scope?<th:block>rather than nested<tr>s, but it should work either way. I'm just saying that the error you are getting can't be explained by the code you have (as far as i can tell).