Search code examples
javaspring-bootthymeleaf

Null index exception in thymeleaf with a filled list


I have a list on the java side which is not empty. But on the front end I have a null index exception. Do you have any idea?

I have this code :

<div th:if="!${#strings.endsWith(partitions[__${rowPartitionStat.index}__].nom,'_in')}">

I've also try this code without success :

<div th:if="!${#strings.endsWith(*{partitions[__${rowPartitionStat.index}__].nom},'_in')}">

In the last case I have a parsing exception so I think the first one is better.

The first one cause this exception :

2017-12-14 15:23:28.937 ERROR 8272 --- [nio-8990-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#strings.endsWith(partitions[0].instances[0].natures[0].nom,'_in')" (topologieCles:155)] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1012E:(pos 9): Cannot index into a null value
    at org.springframework.expression.spel.ast.Indexer.getValueRef(Indexer.java:142) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.Indexer.getValueInternal(Indexer.java:89) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:57) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getArguments(MethodReference.java:154) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getValueRef(MethodReference.java:71) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:66) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:267) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:139) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:154) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

whereas few lines later this line works fine :

<label th:text="*{partitions[__${rowPartitionStat.index}__].nom}" class="col-sm-4 control-label">...</label>

Here is an extract from the pom.xml :

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Solution

  • *{...} expressions behave differently than ${...} expressions. I can't tell exactly (you haven't posted enough html to be sure), but I'm guessing this expression would work for you:

    <div th:if="!${#strings.endsWith(#object.partitions[__${rowPartitionStat.index}__].nom,'_in')}">
    

    The reason you're getting the null pointer access, is because partitions is probably a property on your th:object rather than a model attribute. That's fine for *{...} expressions, but for ${...} expressions you have to provide the entire path -- which is why I'm using the special variable #object.