代码之家  ›  专栏  ›  技术社区  ›  en Peris

EL1012E:无法索引为空值

  •  0
  • en Peris  · 技术社区  · 7 年前

    我有一个百里香模板里的代码

    var theList = /*[[${route}]]*/
    
    
                                for (i = 0; i < theList.length; i++) {
                                     var coordinate = [/*[[${theList[i].longitude}]]*/,/*[[${theList[i].latitude}]]*/];
                                     coordinates2.push(coordinate);
                             }
    

    但是当物体 ${route} 不为空,是我放在控制器中的空列表:

    List<Coordinate> route = new ArrayList<Coordinate>();
                model.addAttribute("route", route);
    

    我得到这个错误:

    org.springframework.expression.spel.SpelEvaluationException: EL1012E: Cannot index into a null value
        at org.springframework.expression.spel.ast.Indexer.getValueRef(Indexer.java:132)
        at org.springframework.expression.spel.ast.Indexer.getValueInternal(Indexer.java:89)
        at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:57)
        at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
        at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:121)
        at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:324)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Metroids    7 年前

    不能混合使用javascript和thymeleaf变量。变量 i 定义在 JavaScript for循环( for (i = 0; i < theList.length; i++) ,但你试图在 胸腺 表达。你得把它们分开。

    var theList = /*[[${route}]]*/ [];
    
    for (i = 0; i < theList.length; i++) {
        var coordinate = [theList[i].longitude, theList[i].latitude];
        coordinates2.push(coordinate);
    }