mvel
mvel copied to clipboard
NullPointerException in MVELRuntime.execute()
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.
Currently, I have to workaround it with @if @else like this: @if{name == empty}@else{}@{name}@end{}