Nuñito de la Calzada Nuñito de la Calzada - 9 months ago 62
Java Question

Thymeleaf - Spring . Accessing a object property inside another object

I have this classes

public class Guardian {

public Guardian() {
super();
}

private Long id;

private String name;
..
}

public class AlarmNotification {


private Long id;


private Guardian guardian;
}


and in my Thymeleaf template

<td class="col_name" th:text="${alarmNotification.guardian.name}"></td>


But I got this Exception

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null

Answer Source

guardian is null. Add ternary operator for processing this case :

<td class="col_name" th:text="${alarmNotification.guardian == null ? '' : alarmNotification.guardian.name}"/>