Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FreeMarker template error: The following has evaluated to null or missing | BUT NOT TRUE

The error I'm facing is so weird. Everything looks fine, but I get this error when the browser sends the GET request to the server. What I'm trying to do is actually catching the HTTP parameters, save them in an object saved in an ArrayList sending to a Freemarker template.

Could you please help me? Thanks a lot.

The error:

freemarker.log._JULLoggerFactory$JULLogger error SEVERE: Error executing FreeMarker template FreeMarker template error: The following has evaluated to null or missing: ==> item.lat1 [in template "view/result.ftl" at line 18, column 15]

freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131) at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:355) at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82) at freemarker.core.DollarVariable.accept(DollarVariable.java:41) at freemarker.core.Environment.visit(Environment.java:324) at freemarker.core.MixedContent.accept(MixedContent.java:54) at freemarker.core.Environment.visitByHiddingParent(Environment.java:345) at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:268) at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:194) at freemarker.core.Environment.visitIteratorBlock(Environment.java:572) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:78) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:64) at freemarker.core.Environment.visit(Environment.java:324) at freemarker.core.MixedContent.accept(MixedContent.java:54) at freemarker.core.Environment.visit(Environment.java:324) at freemarker.core.Environment.process(Environment.java:302) at freemarker.template.Template.process(Template.java:325) at spark.template.freemarker.FreeMarkerEngine.render(FreeMarkerEngine.java:71) at controller.App.lambda$main$1(App.java:57) at spark.RouteImpl$1.handle(RouteImpl.java:58) at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:162) at spark.webserver.JettyHandler.doHandle(JettyHandler.java:61) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119) at org.eclipse.jetty.server.Server.handle(Server.java:517) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:302) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:245) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572) at java.lang.Thread.run(Thread.java:745)

[qtp285763673-17] ERROR spark.webserver.MatcherFilter - java.lang.IllegalArgumentException: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing: ==> item.lat1 [in template "view/result.ftl" at line 18, column 15]

.. But true, because I do print the Array and it works fine!

  final FreeMarkerEngine freeMarkerEngine = new FreeMarkerEngine();
    final Configuration freeMarkerConfiguration = new Configuration();
    freeMarkerConfiguration.setTemplateLoader(new ClassTemplateLoader(App.class, "/"));
    freeMarkerEngine.setConfiguration(freeMarkerConfiguration);

    get("/rest", (request, response) -> {
        Double lat1 = Double.parseDouble(request.queryParams("lat1") != null ? request.queryParams("lat1") : "anonymous");
        Double lon1 = Double.parseDouble(request.queryParams("lon1") != null ? request.queryParams("lon1") : "anonymous");
        Double lat2 = Double.parseDouble(request.queryParams("lat2") != null ? request.queryParams("lat2") : "anonymous");
        Double lon2 = Double.parseDouble(request.queryParams("lon2") != null ? request.queryParams("lon2") : "anonymous");

        if (shouldReturnHtml(request)) {
            response.status(200);
            response.type("text/html");
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("list",loadTheList(lat1,lon1,lat2,lon2));
            return freeMarkerEngine.render(new ModelAndView(attributes, "/view/result.ftl"));
        }
        else {
            response.status(200);
            response.type("application/json");
            return null;
        }
    });


}

private static boolean shouldReturnHtml(Request request) {
    String accept = request.headers("Accept");
    return accept != null && accept.contains("text/html");
}
public static ArrayList<Bereken> loadTheList(double lat1, double lon1, double lat2, double lon2) {
    ArrayList<Bereken> list = new ArrayList<>();
    list.add(new Bereken(lat1,lon1,lat2,lon2));
    return list;
}

And result.ftl:

      <#list list as item>
        <h2>${item.lat1}</h2>
        <h2>${item.lon1}</h2>
        <h2>${item.lat2}</h2>
        <h2>${item.lon2}</h2>
    </#list>
like image 671
Aziz Avatar asked Mar 04 '16 21:03

Aziz


2 Answers

I know this is an old question, but I just bumped into this. Freemarker will also complain that the value "is null or missing" if you pass it an instance of a private class (which I just tried). If you pass in a Java Bean class, it needs to be public. The error message is very misleading in this case.

like image 61
Alan47 Avatar answered Sep 29 '22 02:09

Alan47


You are adding 1 instance of the Bereken class to the "list". So i would assume the Bereken class does not provide public properties lat1, lat2, lon1, lon2 or coresponding getter methods.

like image 34
J.K.Lauren Avatar answered Sep 29 '22 02:09

J.K.Lauren