Skip to content

SpEL throws exception when accessing property on nonexistent variable #34791

@vampireslove

Description

@vampireslove
    @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)

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: invalidAn issue that we don't feel is valid

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions