java - EL1012E: 无法索引到空值
问题描述
我在 Thymeleaf 模板中有这段代码
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)
解决方案
您不能混合使用 JavaScript 和 Thymeleaf 变量。该变量i
在JavaScript for 循环 ( for (i = 0; i < theList.length; i++)
) 中定义,但您正试图在Thymeleaf表达式中使用它。您需要将它们分开。
var theList = /*[[${route}]]*/ [];
for (i = 0; i < theList.length; i++) {
var coordinate = [theList[i].longitude, theList[i].latitude];
coordinates2.push(coordinate);
}
推荐阅读
- caching - 自动过期字典
- jmeter - 如何获得 JMeter 线程组的总体通过/失败结果
- c# - 是否有快速的方法或软件来查看 C# Window Form 事件的触发顺序?
- tensorflow - 如何在没有权重和偏差的情况下将预训练模型加载到 Keras 中?
- ajax - 从 Ajax 返回布尔值
- python - 我有一个包含字符串和浮点数的列表,如何按降序对数字进行排序并将列表打印到标准输出
- python - Python正则表达式从字符串中提取数字
- c# - ispostback 上 Gridview 中的标头 DDL
- android - 无法运行可以在 Android 应用程序中运行汇编代码的示例代码
- c# - 使用asp.net c#获取带有alt键代码符号的网页源代码