0

How to solve this error? I'm using thymeleaf together with spring and there's an error when parsing the following html segment.

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'items' cannot be found on null When I add something to the cart it works. The problem is when it's empty.

`

<tr th:each="item : ${session.shoppingCart.items}">
    <td th:text="${item.book.id}"></td>
        <td th:text="${item.book.title}"></td>
        <td><span th:text="${item.book.price}"></span>cash</td>
        <td>
            <form action="#" th:action="@{/cart/update}" method="post">
                <input type="hidden" th:value="${item.book.id}" name="id"/>
                <input type="number" min="1" th:value="${item.quantity}" name="qty"/>
                <button type="submit">UPDATE</button>
            </form>
        </td>
        <td><span th:text="${item.subTotal}"></span>cash</td>
        <td>
            <form action="#" th:action="@{/cart/remove}" method="post">
                <input type="hidden" th:value="${item.book.id}" name="id"/>
                <button type="submit">remove</button>
            </form>
        </td>
    </tr>

`

gemba09
  • 1
  • 2
  • The error says `${session.shoppingCart.items}` is null or doesn't exist, how and when do you initialize it? – WoAiNii May 01 '20 at 14:25
  • When I add something to the cart it works. The problem is when it's empty. – gemba09 May 01 '20 at 14:35
  • Can you try what suggested [here](https://stackoverflow.com/questions/20636456/using-thymeleaf-when-the-value-is-null)? (please also add that detail to your question) – WoAiNii May 01 '20 at 14:41

3 Answers3

0

In your comment, you said that if you added something to the cart it works, this means that shoppingCart is exist in the session scope, but shoppingCart didn't have any items in it.

All you need to do is first check if items is exist. (If it doesn't exist, you don't have to show it!)

<div th:if="${!session.shoppingCart.items}">
    your code
</div>
Michael Ouyang
  • 1,767
  • 1
  • 11
  • 19
0

You can use th:unless also for this and put your code under div with this attribute like :

<div class="itemslist" th:unless="${#lists.isEmpty(session.shoppingCart.items)}">

<tr th:each="item : ${session.shoppingCart.items}">
    <td th:text="${item.book.id}"></td>
        <td th:text="${item.book.title}"></td>
        <td><span th:text="${item.book.price}"></span>cash</td>
        <td>
            <form action="#" th:action="@{/cart/update}" method="post">
                <input type="hidden" th:value="${item.book.id}" name="id"/>
                <input type="number" min="1" th:value="${item.quantity}" name="qty"/>
                <button type="submit">UPDATE</button>
            </form>
        </td>
        <td><span th:text="${item.subTotal}"></span>cash</td>
        <td>
            <form action="#" th:action="@{/cart/remove}" method="post">
                <input type="hidden" th:value="${item.book.id}" name="id"/>
                <button type="submit">remove</button>
            </form>
        </td>
    </tr>
</div>

Check this reference

Hemant
  • 1,403
  • 2
  • 11
  • 21
  • It's doesn't work org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#lists.isEmpty(session.shoppingCart.items)" – gemba09 May 01 '20 at 15:07
0

Now such an error has appeared.

     org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "!session.shoppingCart.items"
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'items' cannot be found on null

  <div th:if="${!session.shoppingCart.items}">
    <tr th:each="item : ${session.shoppingCart.items}">
        <td th:text="${item.book.id}"></td>
            <td th:text="${item.book.title}"></td>
            <td><span th:text="${item.book.price}"></span>cash</td>
            <td>
                <form action="#" th:action="@{/cart/update}" method="post">
                    <input type="hidden" th:value="${item.book.id}" name="id"/>
                    <input type="number" min="1" th:value="${item.quantity}" name="qty"/>
                    <button type="submit">UPDATE</button>
                </form>
            </td>
            <td><span th:text="${item.subTotal}"></span>cash</td>
            <td>
                <form action="#" th:action="@{/cart/remove}" method="post">
                    <input type="hidden" th:value="${item.book.id}" name="id"/>
                    <button type="submit">remove</button>
                </form>
            </td>
        </tr>
    </div>
gemba09
  • 1
  • 2