mikebrock / mvel

MVEL (MVFLEX Expression Language)
http://mvel.codehaus.org/
Apache License 2.0
50 stars 14 forks source link

NullPointerException in MVELRuntime.execute() #18

Open binghuawu opened 13 years ago

binghuawu commented 13 years ago

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 commented 13 years ago

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