mvel icon indicating copy to clipboard operation
mvel copied to clipboard

NullPointerException in MVELRuntime.execute()

Open binghuawu opened this issue 13 years ago • 1 comments

Hi Mike,

It seems the version after 2.0.19 (excluding) will experiencing a NullPointerException. Here is the test case to reproduce the problem:

public class X { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } }

public static void main(String... args) {
    String exp = "@{name == empty ? 'N/A' : name }";

    CompiledTemplate t = TemplateCompiler.compileTemplate(exp);

    Object o = TemplateRuntime.execute(t, new X());   // This line will throw the exception
    System.out.println(o);

} And the stack trace: java.lang.NullPointerException at org.mvel2.MVELRuntime.execute(MVELRuntime.java:59) at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:128) at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:118) at org.mvel2.MVEL.executeExpression(MVEL.java:928) at org.mvel2.templates.res.CompiledTerminalExpressionNode.eval(CompiledTerminalExpressionNode.java:41) at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:286) at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:282) at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:244) at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:164)

Version 2.0.19 or earlier versions don't have such problem.

binghuawu avatar Jul 20 '11 07:07 binghuawu

Currently, I have to workaround it with @if @else like this: @if{name == empty}@else{}@{name}@end{}

binghuawu avatar Jul 21 '11 05:07 binghuawu