1

I am trying to iterate over a list that contains another list.

@Entity
@Table(name = "session")
public class TrainingSession implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@OneToMany(cascade = CascadeType.ALL)
private List<ExerciseWrapper> exercises;

@Column
@DateTimeFormat(pattern = "YYYY/MM/DD")
private Date sessionDate;
}

Adding list to the Model:

    @RequestMapping("/sessions")
public String getAllSession(Model model){
    List<TrainingSession> trainingSessionList = trainingSessionService.getAllTrainingSessions();
    model.addAttribute("sessionList", trainingSessionList);
    System.out.println(trainingSessionList);
    return PREFIX+"sessions";
}

Iteration:

<th:block th:each="trainingSession : ${sessionList}">
<tr><td th:text="${trainingSession.id}"></td></tr>
<tr>
    <th:block th:each="exerciseWrapper : *{trainingSession.exercises}">
        <tr><td th:text="${exerciseWrapper.id}"></td></tr>
        <tr><td th:text="${exerciseWrapper.exercise.name}"></td></tr>
    </th:block>

</tr>

This results in error:

Property or field 'id' cannot be found on null

How to access list of exercises?

Artur
  • 111
  • 11

2 Answers2

3

Problem is here: *{trainingSession.exercises}. Here exerciseWrapper is null and could not find id from null. Here you used * instead of $

Try with this:

<th:block th:each="trainingSession : ${sessionList}">
<tr><td th:text="${trainingSession.id}"></td></tr>
<tr>
    <th:block th:each="exerciseWrapper : ${trainingSession.exercises}">
        <tr><td th:text="${exerciseWrapper.id}"></td></tr>
        <tr><td th:text="${exerciseWrapper.exercise.name}"></td></tr>
    </th:block>

</tr>

If there is possiblilty of exerciseWrapper is null then add th:if.

With null check:

<th:block th:each="trainingSession : ${sessionList}">
<tr><td th:text="${trainingSession.id}"></td></tr>
<tr>
    <th:block th:each="exerciseWrapper : ${trainingSession.exercises}">
        <tr><td th:if="${exerciseWrapper!=null}" th:text="${exerciseWrapper.id}"></td></tr>
        <tr><td th:if="${exerciseWrapper!=null}" th:text="${exerciseWrapper.exercise.name}"></td></tr>
    </th:block>

</tr>
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39
  • It seems like exerciseWrapper is null indeed. Which is strange, because when I debug the controller, the exerciseWrapper exists.
    . Also, the result of
    ` ` . is . `TrainingSession{id=16, exercises=[ExerciseWrapper{id=17, exercise=Exercise{id=18, name='Highbar Squat', description='More difficult version of squat'}, weight=115.0, reps=5, rpe=9, notes='Everython okay'}], sessionDate=2018-08-11 12:39:15.093}`
    – Artur Aug 11 '18 at 19:06
  • I said where is your problem. Change your thymeleaf – GolamMazid Sajib Aug 12 '18 at 01:46
  • you used * instead of $... *{trainingSession.exercises} will be ${trainingSession.exercises} – GolamMazid Sajib Aug 12 '18 at 03:29
0

You should check first if the information is read completely from database. Try to debug what you put into your model. If it´s already null there, it´s an Hibernate problem.

I think the object you are getting from the first iteration contains a list containing a null element. For dealing with null in Thymeleaf check this question: Using Thymeleaf when the value is null

Eric
  • 123
  • 1
  • 10
  • It's not empty. I've debugged it: `[TrainingSession{id=16, exercises=[ExerciseWrapper{id=17, exercise=Exercise{id=18, name='Highbar Squat', description='More difficult version of squat'}, weight=115.0, reps=5, rpe=9, notes='Everython okay'}], sessionDate=2018-08-11 12:39:15.093}]` – Artur Aug 11 '18 at 13:08