Docly Developers
Knowledge base Pitfall Workaround available

Write fails after process in the same code block

In shortOnce process() has run inside a #{ }# code block, a later write() call in that same block fails with "'write' method call failed with error 'Object reference not set to an instance of an object.'" and the page answers with an error. write() in a later, separate code block still works, and so does a #expression#. Build the markup as a string in the block and emit it with an expression after the block.
Applies to HASH filesTemplatesJavaScript
  1. What you'll see

    A .hash page or display template that renders an example with process() answers with an error page instead of the content:

    Error: 'write' method call failed with error 'Object reference not set to an instance of an object.'

    The reported line is a write() call that comes after the process() call in the same #{ }# block. The same write() worked before process() was added, and every write() earlier on the page still works.

  2. What's actually happening

    Observed on this site on 2026-10-29, in the display template for the function reference. One code block ran each example through process() and then wrote the tabs that show it:

    #{
        let out = "";
        try { out = process(Code, data); } catch (e) { out = ""; }
        write('<h2>Example</h2>');   // fails: 'write' method call failed
    }#

    Every function page answered with the error. What still worked on the same page:

    • write() calls in code blocks before the one that called process().
    • write() in a later, separate code block — here an included template at the end of the article.
    • A #expression# after the block, emitting a variable the block had built.

    So the failure is tied to calling write() in the same block after process() has returned. The old version of the template never hit it, because it emitted the example output with expressions in the markup rather than with write().

  3. What to do

    Build the markup as a string and emit it with an expression after the block. Declare the variable at the top level of the block so the expression can see it:

    #{
        let out = "";
        try { out = process(Code, data); } catch (e) { out = ""; }
        let html = '<h2>Example</h2><pre><code>' + docly.htmlEncode(out) + '</code></pre>';
    }#
    #html#

    Alternatively, close the block after process() and do the remaining write() calls in a new #{ }# block.

    • Encode what you emit — an expression outputs the string verbatim (see Hash expressions do not escape HTML).
    • A page that calls process() is easy to break this way and the error takes the whole page down, so check it with a cache-busted request after every change, and read docly console for the error and its line.

Last reviewed 29 Oct 2026