Skip to content

SpEL throws exception when accessing property on nonexistent variable #34791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vampireslove opened this issue Apr 22, 2025 · 3 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@vampireslove
Copy link

vampireslove commented Apr 22, 2025

    @Test
    public void testSpel(){
        String expression = "#name == 'joy' || #age > 10 || #ab.type == 1 || #a == 10";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("age", 8);
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariables(paramMap);
        ExpressionParser parser = new SpelExpressionParser();
        org.springframework.expression.Expression exp =  parser.parseExpression(expression);
        Object result = exp.getValue(context);
        System.out.println(result);
    }

I expect the result output to be false, but an exception is thrown. Can this situation be optimized, when encountering non-existent data, it should be judged as unsatisfied instead of throwing an exception?

ERROR:

Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'type' cannot be found on null
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:219)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:106)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:53)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:412)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:93)
	at org.springframework.expression.spel.ast.OpEQ.getValueInternal(OpEQ.java:42)
	at org.springframework.expression.spel.ast.OpEQ.getValueInternal(OpEQ.java:32)
	at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:212)
	at org.springframework.expression.spel.ast.OpOr.getBooleanValue(OpOr.java:56)
	at org.springframework.expression.spel.ast.OpOr.getValueInternal(OpOr.java:51)
	at org.springframework.expression.spel.ast.OpOr.getValueInternal(OpOr.java:37)
	at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:114)
	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:273)
@vampireslove
Copy link
Author

org.springframework spring-expression 6.2.6

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 22, 2025
@sbrannen sbrannen changed the title spring-expression Don't throw an exception when the data doesn't exist. SpEL throws exception for non-existent variable Apr 22, 2025
@sbrannen sbrannen self-assigned this Apr 22, 2025
@sbrannen sbrannen added status: invalid An issue that we don't feel is valid in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 22, 2025
@sbrannen sbrannen changed the title SpEL throws exception for non-existent variable SpEL throws exception when accessing property on nonexistent variable Apr 22, 2025
@sbrannen
Copy link
Member

Hi @vampireslove,

Congratulations on submitting your first issue for the Spring Framework! 👍

The behavior you have encountered is expected.

Any attempt to access a property on a null reference (or a nonexistent variable, which is the case in your example) will result in a SpelEvaluationException.

However, you can make your expression return false if you use the null-safe navigation operator (?.) which evaluates to null for #ab?.type in your example.

@Test
void testSpel() {
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.setVariable("age", 8);

	String expression = "#name == 'joy' || #age > 10 || #ab?.type == 1 || #a == 10";
	Expression exp = parser.parseExpression(expression);

	assertThat(exp.getValue(context, Boolean.class)).isFalse();
}

The above modified version of your example passes.

In light of that, I am closing this as "works as designed".

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2025
@vampireslove
Copy link
Author

@sbrannen Thank you very much for your answer! But #ab.type == 1 than #ab?. type == 1 is simpler 。 This is especially true in the case of multi-level expressions .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants