Issue
I have this piece of code in a Thymeleaf template
var theList = /*[[${route}]]*/
for (i = 0; i < theList.length; i++) {
var coordinate = [ /*[[${theList[i].longitude}]]*/ , /*[[${theList[i].latitude}]]*/ ];
coordinates2.push(coordinate);
}
when the object ${route}
is not null, but an empty List that I pass through the controller:
List<Coordinate> route = new ArrayList<Coordinate>();
model.addAttribute("route", route);
I get this error:
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)
Solution
You can't mix JavaScript and Thymeleaf variables. The variable i
is defined in the JavaScript for loop (for (i = 0; i < theList.length; i++)
), but you are trying to use it in a Thymeleaf expression. You need to keep them separate.
var theList = /*[[${route}]]*/ [];
for (i = 0; i < theList.length; i++) {
var coordinate = [theList[i].longitude, theList[i].latitude];
coordinates2.push(coordinate);
}
Answered By - Metroids