【问题标题】:Thymeleaf: Getting Property or field cannot be found on null. Iteration of list inside a listThymeleaf:在 null 上找不到获取属性或字段。列表内列表的迭代
【发布时间】:2019-03-11 04:23:11
【问题描述】:

我正在尝试使用 thymeleaf 在我的 html 中迭代交易列表(在 AllTransaction 对象内)。我调试并且对象在添加到模型时被正确填充。但是在尝试迭代时给出了这个异常。需要在另一个列表 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);

堆栈跟踪

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

【问题讨论】:

  • 你也可以添加堆栈跟踪吗?
  • @soorapadam 已添加
  • 那么你在 Transactions 对象中定义了变量名Broker 还是broker
  • @soorapadman 公共字符串代理;
  • 好的只是想确保当您访问错误的属性名称时会出现此错误。根据 java 约定,请不要定义这样的名称。我怀疑您的事务对象可能有问题,可能是 getter 和 setter 。如果可能的话,也分享一下。\

标签: spring thymeleaf


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2018-03-31
    • 2018-08-07
    相关资源
    最近更新 更多