# How to handle null values in Thymeleaf

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Jan 21, 2020. Topics: Thymeleaf.
Canonical URL: https://attacomsian.com/blog/thymeleaf-handling-null-values

> A quick guide to learn how to handle null values in Thymeleaf templates.

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](https://attacomsian.com/blog/spring-boot-thymeleaf-example).

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

## Using Safe Navigation Operator &mdash; `?`

The [safe navigation operator](https://en.wikipedia.org/wiki/Safe_navigation_operator) &mdash; also known as **optional chaining operator**, **safe call operator**, and **null-conditional operator** &mdash; 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](https://attacomsian.com/blog/thymeleaf-conditions): `th:if` & `th:unless` attributes.

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

```markup
<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:

```markup
<p th:text="${post.author.email}" th:if="${post ne null and post.author 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:

<p class="file-desc"><span>Post.java</span></p>

```java
public class Post implements Serializable {

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

    // constructor, getters, and setters removed for brevity
}
```

<p class="file-desc"><span>Author.java</span></p>

```java
public class Author implements Serializable {

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

    // constructor, getters, and setters removed for brevity
}
```

## Using Elvis Operator &mdash; `?:`

Another way to handle null values in the Thymeleaf templates is by using the [Elvis operator](https://attacomsian.com/blog/thymeleaf-conditions#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:

```markup
<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:

```markup
<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:

```markup
<p th:text="${post.author.name} ?: '(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:

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

## Conclusion

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](https://attacomsian.com/blog/spring-boot-thymeleaf-example)
