0

I am having some trouble with Thymeleaf and Spring MVC. I have an entity class that is managed by a repository connected to a SQL database.

In my controller, I am adding a list of returned entities to my Model and trying to access these in my template. The problem is, when one of the fields of that entity is null, when trying to access that field it returns a SpEL error in the following format.

Exception evaluating SpringEL expression: "entity.phoneNumber" (template: "index" - line 13, col 17)

Like I mentioned previously, this ONLY happens when one of the fields of the Entity is null. I tried using the safe navigation operator like this...

entity?.phoneNumber

But it is the attribute that is null, not the entity itself.

I have also tried using something like this, but this also returns an error as it can't even find the attribute to see if it is null.

<span th:if="${entity.phoneNumber != null}" th:text="${entity.phoneNumber}">Phone Number</span>

The controller looks like this.

@Controller
public class IndexController {

@Autowired
CustomerService customerService;

@GetMapping
public String index(Model model) {
        List<ActiveCustomersEntity> entities = customerService.getAllCustomers();
        model.addAttribute("entities", entities);
        return "index";
    }
}

Now my template looks like this.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>title</title>
</head>

<body>
    <table>
        <tr th:each="entity : ${entities}">
            <td th:text="${entity.customerFormattedNm}">Customer Name</td>
            <td th:text="${entity.accountStatusCd}">Account Status Code</td>
        </tr>
    </table>
</body>

</html>

I have checked repeatedly for mis-spellings. When I only look at attributes that are guaranteed to have a value, everything works as intended. It is only attributes that have a chance to be null that cause issues.

Any help would be appreciated!

M Boi
  • 1
  • 2
  • This is a very strong indication that your entity is in fact null. Try inserting a `` and see what you get. – chrylis -cautiouslyoptimistic- Mar 04 '19 at 19:17
  • Hello @chrylis , thank you for the response. When changing it to just be ${entities} as it is a List of entities, I end up with a massive array on the page full of (company info removed) "com.****.mvctest.model.entity.***.ActiveCustomersEntity@d17ec6f". However, I know the model itself is not null because as I mentioned before I can access all the fields that are guaranteed to have a non-null value. Furthermore, I have tried getting one entity at a time, and the ones with all fields present work with no issue, while ones that have null fields throw the SpEL error. – M Boi Mar 04 '19 at 20:07
  • If `${entity}` is not null, then the expression `${entity.phoneNumber}` will NEVER throw an error (in normal getter and setter use cases) -- it will simply output an empty string. There is something else going on here and we don't have enough information to help, based on the code you've shared. – Metroids Mar 04 '19 at 20:57
  • I am using project Lombok to automatically create my getters and setters. I wonder if this could be related to the issue. I am also going to try mapping my entity to a DTO before passing it to the model, and see if that helps. I am going to try a few more things tomorrow and will update this thread. Thanks for your answers so far! – M Boi Mar 04 '19 at 21:30
  • Please take a look at this link https://stackoverflow.com/questions/49845057/spring-bootthymeleaf-th-not-able-to-resolve-a-spring-el-expression I beleive if you do generate your getter/setter methods without Lombok then everything should be fine based on your current posted code. – Ahmet Mar 05 '19 at 08:05

1 Answers1

0

Providing an update since I figured out what was causing this. The entity class has getters which use the trim() function to remove the trailing whitespace from the database since it is using char instead of varchar. Null values can't be trimmed, so I simply had to change my getters to account for null values.

return phoneNumber == null ? null : phoneNumber.trim();
M Boi
  • 1
  • 2