胸腺:在null上找不到财产或字段.列表中列表的迭代



我正在尝试使用胸腺中的HTML中迭代交易列表(在Altransaction对象内部)。我进行了调试,并且在添加到模型的同时,对象被正确填充。但是,在尝试迭代时会给出这个例外。需要在其他列表中迭代交易列表。

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

解决方案: - 这是我用来解决问题的解决方案

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

最新更新