EL1012E:无法索引为空值

时间:2018-06-18 09:27:10

标签: java spring spring-mvc spring-boot thymeleaf

我在Thymeleaf模板中有这段代码

${route}

但是当对象List<Coordinate> route = new ArrayList<Coordinate>(); model.addAttribute("route", route); 不为null时,我将一个空列表放在控制器中:

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

1 个答案:

答案 0 :(得分:1)

您不能混合使用JavaScript和Thymeleaf变量。变量for (i = 0; i < theList.length; i++) JavaScript for循环(var theList = /*[[${route}]]*/ []; for (i = 0; i < theList.length; i++) { var coordinate = [theList[i].longitude, theList[i].latitude]; coordinates2.push(coordinate); } )中定义,但您尝试在 Thymeleaf 表达式中使用它。你需要将它们分开。

public class Person
{
    public int Id { get; set; }

    [Required(ErrorMessage = "First name is required")]
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? DateOfBirth { get; set; }
}