title: Camel Freemarker Example - Template Returns Null
body: <p><strong>The Problem:</strong></p>
  
  <p>I am trying to run the Camel Freemarker Example from the Camel website <a href="http://camel.apache.org/freemarker.html" rel="nofollow noreferrer">here</a>, so I can understand how they work together. However, I'm having a bit of trouble. </p>
  
  <p>As you can see in the code below, everything is pretty much the same as what's in the documentation. I changed where the template goes, the "from" endpoint directory, and where the ftl file is. When I run the code, I get the following error:</p>
  
  <pre><code>FreeMarker template error:
  The following has evaluated to null or missing:
  ==&gt; headers.lastName [in template...]
  </code></pre>
  
  <p>Stack trace adds tips to find errors, then continues...</p>
  
  <pre><code>FTL Stack trace ("~" means nesting-related):
   - Failed at: ${headers.lastName} [in template...]
  </code></pre>
  
  <p><strong>My Logic/What I've Tried:</strong></p>
  
  <p>Now, I don't understand why headers.lastName is being found null. I thought that the <code>msg.setHeader("lastName", "Ibsen");</code> part was supposed to set that to Ibsen. I thought maybe I should try setting the name to <code>msg.setHeader("headers.lastName", "Ibsen");</code>, but that didn't work.</p>
  
  <p>After that, I am not quite sure what else to look for. I am fairly new to both Camel and Freemarker. I think that something is not getting sent during the exchange, but I'm not sure what.
  Can someone please explain why this isn't working?</p>
  
  <p>Any advice is appreciated.</p>
  
  <p><strong>Code:</strong></p>
  
  <p>Here is the ftl:</p>
  
  <pre><code>Dear ${headers.lastName}, ${headers.firstName}
  
  Thanks for the order of ${headers.item}.
  
  Regards Camel Riders Bookstore
  ${body}
  </code></pre>
  
  <p>Here is the Java code:</p>
  
  <pre><code>public class FreemarkerExample  extends CamelTestSupport{
  
      private static Configuration cfg = null;
  
      static {
          cfg = new Configuration(new version("2.3.0"));
      }
  
      public static void main(String[] args) throws Exception {
          Main main = new Main();
          main.enableHangupSupport();
          main.addRouteBuilder(new FreemarkerExample().new MyRouteBuilder());
          main.run();
      }
  
      private Exchange createLetter() throws IOException, TemplateException{
          Exchange exchange = context.getEndpoint("direct:a").createExchange();
  
          Message msg = exchange.getIn();
          msg.setHeader("firstName", "Claus");
          msg.setHeader("lastName", "Ibsen");
          msg.setHeader("item", "Camel in Action");
          msg.setBody("PS: Next beer is on me, James");
  
          return exchange;
      }
  
      @Test
      public void testFreemarkerLetter() throws Exception {
          MockEndpoint mock = getMockEndpoint("mock:result");
          mock.expectedMessageCount(1);
          mock.message(0).body().contains("Dear Ibsen, Claus");
          mock.message(0).body().contains("Thanks for the order of Camel in Action.");
  
          template.send("file:C:\\freemarkerExampleFile", createLetter());
  
          mock.assertIsSatisfied();
      }
  
      class MyRouteBuilder extends RouteBuilder {
  
          public void configure() throws Exception {
              from("file:C:\\freemarkerExampleFile\\templates")
                      .to("freemarker:file:C:\\freemarkerExampleFile\\templates\\letter.ftl")
                      .to("mock:result");
          }
  
      }
  
  }
  </code></pre>