0

I'm trying to populate a thymeleaf template with some data from Spring Boot. What I'm trying to do is this

<tr th:if="${group.organization}">
    <td class="col_title"><b>Organization:</b></td>
    <td class="organization-field-content" th:text="${group.organization}"></td>
</tr>

I've tried both the solutions proposed here: Thymeleaf: show text if the attribute and property exists and, following the render order of thymeleaf, since the group.organization is null, the entire inner tds should not be displayed.

Still there a problem because Thymeleaf complains saying

Servlet.service() for servlet [dispatcherServlet] in context with path [] 
threw exception [Request processing failed; nested exception is 
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating 
SpringEL expression: "group.organization" (group)] with root cause
java.lang.NullPointerException: null

I don't understand why this is happening, since the group object exists, just the organization is null

Community
  • 1
  • 1
Kerruba
  • 1,779
  • 1
  • 17
  • 25

3 Answers3

0

You may confirm if really your group object is not null by just dumping it in a span something like <span th:utext="${group}" />

mhasan
  • 3,703
  • 1
  • 18
  • 37
0

Maybe try the following to check whether group and organisation are not null before showing the div.

<tr th:if="${group != null && group.organization != null}">
    <td class="col_title"><b>Organization:</b></td>
    <td class="organization-field-content" th:text="${group.organization}"></td>
</tr>
Mark Ashworth
  • 164
  • 12
0

Thanks for all the replies, turned out the problem was somewhere else in the end.

All the proposed solution were still giving the same problem, and that was because of a method inside the Group Spring-data Model. Indeed the null pointer exception came out of the getOrganization method, which wasn't serializing correctly the null value.

Maybe this can help someone else in the future

Kerruba
  • 1,779
  • 1
  • 17
  • 25