Skip to content

SpEL: Elvis operator throwing NPE in case of an empty base expression [SPR-7209] #11868

@spring-projects-issues

Description

@spring-projects-issues

drekbour opened SPR-7209 and commented

Embedded SpEL expressions throw an NPE if looked-up property-placeholder variable is null.

Using the Elvis operator which is designed to deal with null:
@Value("#{${property1}?:'default'}") private String property2;

This works fine if ${property1} evaluates to a non-null but provides the following if it is null.

Caused by: java.lang.NullPointerException
at org.springframework.expression.spel.ast.SpelNodeImpl.<init>(SpelNodeImpl.java:50)
at org.springframework.expression.spel.ast.Elvis.<init>(Elvis.java:33)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatExpression(InternalSpelExpressionParser.java:143)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:114)
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:56)
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:1)
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpressions(TemplateAwareExpressionParser.java:128)
at org.springframework.expression.common.TemplateAwareExpressionParser.parseTemplate(TemplateAwareExpressionParser.java:74)
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:64)
at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:119)

The same thing happens if you use a .property file with the following
property1=
property2=#{${property1}?:'default'}

Notes:


Affects: 3.0.2

Issue Links:

Referenced from: commits 101d838

1 votes, 2 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions