Thymeleaf: Получение свойства или поля не может быть найдено на нуле. Итерация списка внутри списка - PullRequest
2 голосов
/ 11 марта 2019

Я пытаюсь перебрать список транзакций (внутри объекта AllTransaction) в моем html-файле с помощью thymeleaf. Я отлажен, и объект добавляется правильно при добавлении в модель. Но дает это исключение при попытке итерации. Необходимо перебрать список транзакций в AllTransactions, который является другим списком.

AllTransactions.java

public class AllTransactions {

    public String tickerName;

    public List<Transactions> transactions;

    public String getTickerName() {
        return tickerName;
    }

    public void setTickerName(String tickerName) {
        this.tickerName = tickerName;
    }

    public List<Transactions> getTransactions() {
        return transactions;
    }

    public void setTransactions(List<Transactions> transactions) {
        this.transactions = transactions;
    }
}

HTML-код

<div th:each="itemx : ${alltxs2}">
  <div th:each="tx : ${itemx.transactions}"> 
    <div th:text="${tx.Broker}">
    </div>
  </div>
</div>

Контроллер

  List<AllTransactions> allTransactions= new ArrayList<AllTransactions>();
            AllTransactions alltraTransactions= new AllTransactions();
            for(String ticker: tickers) {
                transactions = m.makeCall(ticker);
                 alltraTransactions.setTransactions(transactions);
                 alltraTransactions.setTickerName(ticker);
                 allTransactions.add(alltraTransactions);
            }

            model.addAttribute("alltxs2",allTransactions);

StackTrace

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "tx.Broker" (template: "index2" - line 42, col 59)
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at 
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'Broker' cannot be found on null
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:90) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:109) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:328) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    ... 68 common frames omitted

1 Ответ

0 голосов
/ 11 марта 2019

Решение: - Вот решение, которое я использовал для решения проблемы

<div th:each="itemx : ${alltxs}"> 
       <th:block th:each="tx : ${itemx}"> 
       <th:block th:each="x: ${tx.transactions}">
       <div th:text="${x.broker}"></div>
       </th:block>
      </th:block>
   </th:div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...