How to handle null values in Thymeleaf?

In this quick article, we will describe how to avoid NullPointerExceptions during the rendering process of the Thymeleaf template. We will show the simple method to handle unexpected null values.
Learn Thymeleaf at https://www.javaguides.net/p/thymeleaf-tutorial.html.

Safe navigation operator - ?

The Safe Navigation operator (?) is used to avoid an unexpected NullPointerExceptions.
The save navigation operator will simply return null instead of throwing a NullPointerException. We can use this operator in Thymeleaf templates to get rid of null checking conditions using th:if attributes.
In the following example, if the role object is null in user object then throws NullPointerException:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="ISO-8859-1">
    <title>How to handle null values in Thymeleaf?</title>

    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet" />
</head>

<body>
    <div class="container">
        <div class="row">
            <p th:text="${user.role.role}"></p>
        </div>
    </div>
</body>

</html>
Exception:
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'role' cannot be found on null

Fix NullPointerException Issue

In the following example, we will use ? operator to handle nullable role object:
<p th:text="${user?.role?.role}"></p>
The complete Thymeleaf template:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="ISO-8859-1">
    <title>How to handle null values in Thymeleaf?</title>

    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet" />
</head>

<body>
    <div class="container">
        <div class="row">
            <p th:text="${user?.role?.role}"></p>
        </div>
    </div>
</body>

</html>
The handler method in the Spring MVC controller:
 @GetMapping("/demo")
 public String demo(Model model) {
  User user = new User("Ramesh", "[email protected]", null);
  model.addAttribute("user", user);
  return "switch-case";
 }
The User.java model class:
public class User {
    private String userName;
    private String email;
    private Role role;
    public User(String userName, String email, Role role) {
        super();
        this.userName = userName;
        this.email = email;
        this.role = role;
    }
}
The Role.java model class:
public class Role {
 
    private String role;

    public Role(String role) {
        super();
        this.role = role;
    }
}

Related Thymeleaf Tutorials and Examples

  1. Introducing Thymeleaf | Thymeleaf Template | Thymeleaf Template Engine
  2. Thymeleaf Example with Spring Boot
  3. How to Add CSS and JS to Thymeleaf
  4. Add Bootstrap CSS to Thymeleaf
  5. How to handle null values in Thymeleaf?
  6. How to Loop a List by Index in Thymeleaf
  7. Thymeleaf Array Example - Array Index, Array Iteration
  8. Thymeleaf Enums Example
  9. Thymeleaf If Else Condition Example
  10. Thymeleaf Switch Case Example

Comments

  1. This was very helpful. Was stuck on a problem for a couple of hour and this help resolve the issue.

    ReplyDelete

Post a Comment

Leave Comment