Thymeleaf:在 null 上找不到获取属性或字段.列表中列表的迭代 [英] Thymeleaf: Getting Property or field cannot be found on null. Iteration of list inside a list

查看:56
本文介绍了Thymeleaf:在 null 上找不到获取属性或字段.列表中列表的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 thymeleaf 在我的 html 中迭代交易列表(在 AllTransaction 对象中).我调试并在添加到模型时正确填充对象.但是在尝试迭代时给出了这个异常.需要在 AllTransactions 中迭代交易列表,这是另一个列表.

AllTransactions.java

public class AllTransactions {公共字符串股票名称;公开列表<交易>交易;公共字符串 getTickerName() {返回代码名称;}public void setTickerName(StringtickerName) {this.tickerName = 股票代码;}公开列表<交易>获取交易(){退货交易;}public void setTransactions(List transactions) {this.transactions = 交易;}}

html 代码

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

控制器

 ListallTransactions= new ArrayList();AllTransactions alltraTransactions= new AllTransactions();for(字符串股票代码:股票代码){交易 = m.makeCall(ticker);alltraTransactions.setTransactions(transactions);alltraTransactions.setTickerName(ticker);allTransactions.add(alltraTransactions);}model.addAttribute("alltxs2",allTransactions);

堆栈跟踪

org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常:tx.Broker"(模板:index2" - 第 42 行,第 59 行)在 org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]在引起:org.springframework.expression.spel.SpelEvaluationException:EL1007E:在空值上找不到属性或字段经纪人"在 org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]在 org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]在 org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]在 org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]在 org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:90) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]在 org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:109) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]在 org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:328) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]在 org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]...省略了 68 个常用帧

解决方案

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

<th:block th:each="tx : ${itemx}"><th:block th:each="x: ${tx.transactions}"><div th:text="${x.broker}"></div></th:block></th:block>

I am trying to iterate a list of transactions (inside AllTransaction object) in my html using thymeleaf. I debugged and object is getting populated properly while adding to model. But gives this exception when trying to iterate . Need to iterate transactions list in AllTransactions which is another list.

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 code

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

Controller

  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

解决方案

Solution:- Here is the solution i used to solve the problem

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

这篇关于Thymeleaf:在 null 上找不到获取属性或字段.列表中列表的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆