In this quick article, you'll learn how to handle null values in Thymeleaf templates to avoid NullPointerException. Thymeleaf is a modern server-side template engine for both web and standalone Java applications. To learn more about how to use Thymeleaf with Spring Boot, check out this tutorial.

Thymeleaf template engine provides multiple ways to gracefully handle null values in templates.

Using Safe Navigation Operator — ?

The safe navigation operator — also known as optional chaining operator, safe call operator, and null-conditional operator — is a binary operator that returns a null value if its first argument is null; otherwise performs dereferencing operator as specified by the next argument.

The safe navigation operator can be used in Thymeleaf to check whether the reference to an object is null or not, before accessing its fields and methods. It will just return a null value instead of throwing a NullPointerException. This also eliminates the need to manually check if the object is null using conditionals: th:if & th:unless attributes.

The following example demonstrates how you can use safe navigation operator (?) to handle nullable author object:

<p th:text="${post?.author?.email}"></p>

Without safe navigation operator, you have to manually verify that both post and author objects are not null to avoid NullPointerException as shown below:

<p th:text="${}" th:if="${post ne null and ne null}"></p>

When the post or author object is null, the p tag will be empty. The model classes we referenced in the above examples have the following structure:

public class Post implements Serializable {

    private int id;
    private String title;
    private String body;
    private Author author;

    // constructor, getters, and setters removed for brevity

public class Author implements Serializable {

    private long id;
    private String name;
    private String email;

    // constructor, getters, and setters removed for brevity

Using Elvis Operator — ?:

Another way to handle null values in the Thymeleaf templates is by using the Elvis operator or default expression. It is a special kind of conditional expression without a then part.

Elvis operator lets you specify two expressions in one attribute. The first expression is used if it doesn't evaluate to null; otherwise, the second one is used.

Here is an example of an Elvis operator:

<p th:text="${post.body} ?: '(no content)'"></p>

As you can see above, we have used the operator (?:) to specify a default literal value for a name that is display if the evaluation of ${post.body} returns a null or empty value. Therefore, it is equivalent to:

<p th:text="${post.body ne null} ? ${post.body} : '(no content)'"></p>

Note that the Elvis operator can only be used for testing top-level object properties and not for the nested object properties. For example, if you try to use the operator for checking the author object property like below:

<p th:text="${} ?: '(no name found)'"></p>

The Thymeleaf template processor will throw a NullPointerException exception if the author object is null. However, you can combine both optional chaining and Elvis operator together to render a different HTML response based on object values:

<p th:text="${post?.author?.name} ?: '(no name found)'"></p>


In this quick article, we looked at different ways to handle null values in Thymeleaf templates to avoid a NullPointerException during the rendering process. The safe navigation and Elvis operators help us verify that if a certain object or a property is null before we reference its child properties.

Read Next: How to use Thymeleaf with Spring Boot

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.