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?

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

It seems like exerciseWrapper is null indeed. Which is strange, because when I debug the controller, the exerciseWrapper exists.<br/> . Also, the result of<br/> <th:block th:each="trainingSession : ${sessionList}"> <tr><td th:text="${trainingSession}"></td></tr> </tr> . 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}
I said where is your problem. Change your thymeleaf
you used * instead of $... *{trainingSession.exercises} will be ${trainingSession.exercises}
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

1 Comment

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}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.