0

I'm using mvel for the first time. Using @includeNamed works fine when simply running TemplateRuntime.eval.

But if I attempt to use a CompiledTemplate, it's throwing a NPE. Am I doing something wrong? Or is this a bug? I'm using mvel 2.1.4.Final

public class App {
    public static void main(String[] args) {


        TemplateRegistry registry = new SimpleTemplateRegistry();
        registry.addNamedTemplate("world", TemplateCompiler.compileTemplate("world!!!"));

        System.out.println(TemplateRuntime.eval("Eval Hello: @includeNamed{'world'}", null, registry));

        CompiledTemplate ct = TemplateCompiler.compileTemplate("Compile Hello: @includeNamed{'world'}");
        System.out.println(TemplateRuntime.execute(ct, null, registry));
    }
}

And the stacktrace (Note: Eval prints fine):

Eval Hello: world!!!
Exception in thread "main" java.lang.NullPointerException
    at org.mvel2.integration.impl.StackResetResolverFactory.<init>(StackResetResolverFactory.java:15)
    at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
    at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
    at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113)
    at org.mvel2.MVEL.executeExpression(MVEL.java:930)
    at org.mvel2.templates.res.CompiledNamedIncludeNode.eval(CompiledNamedIncludeNode.java:56)
    at org.mvel2.templates.res.TextNode.eval(TextNode.java:42)
    at org.mvel2.templates.res.TextNode.eval(TextNode.java:42)
    at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:285)
    at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:247)
    at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:255)
    at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:187)
    at mveltest.App.main(App.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
nogridbag
  • 3,521
  • 4
  • 38
  • 51

1 Answers1

0

Well, it seems like a bug. I noticed the NPE occurred in StackResetResolverFactory's constructor when variableResolverFactory.someMethod was called.

So as a workaround, I use the execute method which takes a VariableResolverFactory.

Map<String, Object> dummyVariableMap = new HashMap<String, Object>();
VariableResolverFactory dummyResolverFactory = new SimpleVariableResolverFactory(dummyVariableMap);
System.out.println(TemplateRuntime.execute(ct, null, dummyResolverFactory, registry));

Strange, I thought executing a CompiledTemplate with a named template would be a very common use case.

nogridbag
  • 3,521
  • 4
  • 38
  • 51