How to fix: Exception evaluating SpringEL expression

I am trying to save an “employee” model without assigning any “phone” model to it (oneToOne relationship). I have a form, in which i create the employee by adding: firstName, lastName and then a dropdown list from where i can select a phone to assign it. Whenever i do not select anything, the index.html page throws: Exception evaluating SpringEL expression: “employee.phone.getBrand() +’ ‘+ employee.phone.getModel()” (template: “index” – line 31, col 17)

<div align="center">
<table >
    <thead>
    <tr>
        <th>Id Employee</th>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Phone</th>
        <th>Manage</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="employee: ${employeeList} ">
        <td th:text="${employee.id}">Id</td>
        <td th:text="${employee.lastName}">Last Name</td>
        <td th:text="${employee.firstName}">First Name</td>
        <td th:text="${employee.phone.getBrand() +' '+ employee.phone.getModel()} ">Brand</td>
        <td>
            <a th:href="@{'employee/edit/' + ${employee.id}}"><button class="btnEdit"><i class="fa fa-edit"></i></button></a>
            &nbsp &nbsp
            <a th:href="@{'employee/delete/' + ${employee.id}}"><button class="btnDelete"><i class="fa fa-edit"></i></button></a>
        </td>
    </tr>
    </tbody>
</table>
</div>

I mention that the object can be found in DB and the phone_id is null. I want to add some employees with a phone and some without any phone.
The output should be:
Doe John noPhone -> nothing selected from dropdown list.
Doe Jane iPhone 10s – > phone selected.

Do you have any suggestions ?

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

If phone can be null, try to use null-safe accessor ?.. Try something like this employee.phone?.getBrand()