[jira] [Commented] (VELOCITY-966) RuntimeInstance.render throws null pointer exception when trying to render the same AST

"Claude Brisson (Jira)" <[email protected]>
Newsgroups gmane.comp.jakarta.velocity.devel
Message-ID <[email protected]>
    [ https://issues.apache.org/jira/browse/VELOCITY-966?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17878307#comment-17878307 ] 

Claude Brisson commented on VELOCITY-966:
-----------------------------------------

I admit that the API is far from perfect, mostly because of backward compatibility issues.

But the RuntimeInstance.render() method is *not* meant for a reuse of the AST tree. I will add it to the doc.

What you should do, instead, is rely on the Resource and Template API, as follow:

{code}
        final RuntimeInstance runtimeInstance = new RuntimeInstance();
        final VelocityContext context         = new VelocityContext();
        context.put("user", "abc");

        final Template aTemplate = new Template();
        aTemplate.setName("a_template");
        aTemplate.setRuntimeServices(runtimeInstance);
        final SimpleNode parsedTemplate = runtimeInstance.parse(new StringReader("Hello $user !!!"), aTemplate);
        aTemplate.setData(parsedTemplate);
        aTemplate.initDocument();

        //first time using parsed template
        final StringWriter writer = new StringWriter();
        aTemplate.merge(context, writer);
        System.out.println(writer);

        //second time using parsed template
        final StringWriter writer2 = new StringWriter();
        aTemplate.merge(context, writer2);
        System.out.println(writer2);
{code}


> RuntimeInstance.render throws null pointer exception when trying to render the same AST 
> ----------------------------------------------------------------------------------------
>
>                 Key: VELOCITY-966
>                 URL: https://issues.apache.org/jira/browse/VELOCITY-966
>             Project: Velocity
>          Issue Type: Bug
>    Affects Versions: 2.3
>            Reporter: Alex
>            Assignee: Claude Brisson
>            Priority: Major
>
> The objective is to use 
> *RuntimeInstance.parse* to get the parsed AST for some template and cache the AST.
> Then use *RuntimeInstance.render* multiple times to render the text.
>  
> It appears that in version 2.3 {*}RuntimeInstance.{*}{*}render{*} invokes the Node.init call and at the end of the rendering clears the parser and tokens. On the next attempt to render init fails with the NullPointerException because tokens have been cleared.{*}{*}
>  
> So you can not call *RuntimeInstance.render* on the same AST several times
> which seems to be the intent of this method...



--
This message was sent by Atlassian Jira
(v8.20.10#820010)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.