Home > Back-end >  Traversing the data model in a freemarker template List <JavaBean> null pointer exception
Traversing the data model in a freemarker template List <JavaBean> null pointer exception

Time:11-17

Below is my Java code, is through the servlet request, the list collection is through the database to check up, after the test no problem,
Package com. Servlet;

Import the Java. IO. IOException;
Import the Java. IO. PrintWriter;
Import the Java. IO. Writer;
import java.util.ArrayList;
Import the Java. Util. HashMap;
import java.util.List;
Import the Java. Util. The Map;

The import javax.mail. Servlet. ServletException;
The import javax.mail. Servlet. The annotation. WebServlet;
The import javax.mail. Servlet. HTTP. HttpServlet;
The import javax.mail. Servlet. HTTP. It;
The import javax.mail. Servlet. HTTP. HttpServletResponse;

The import com. The entity. The Food;
Import the entity. The News;
The import com. Services. FoodService;
The import com. Services. NewsService;
The import com. Services. Impl. FoodServiceImpl;
The import com. Services. Impl. NewsServiceImpl;

. Import freemarker template. *;
@ WebServlet (urlPatterns={} "/Index" and name="IndexServlet")
Public class IndexServlet extends the HttpServlet {
Private Configuration CFG.

Public void init () {
CFG=new Configuration ();
CFG. SetDefaultEncoding (" utf-8 ");
CFG. SetServletContextForTemplateLoading (getServletContext (),
"WEB - INF/templates");
}


Public void doGet (it request, HttpServletResponse response)
Throws ServletException, IOException {
FoodService FoodService=new FoodServiceImpl ();
NewsService NewsService=new NewsServiceImpl ();
The Map root=new HashMap ();
List List=foodService. GetFoodList ();
Root. The put (" foods ", a list);


//Map Root1=new HashMap (a);
//List List1.=newsService getNewsList ();
//root1. Put (" list1, "list1);

The Template t=CFG. GetTemplate (" index. The FTL ");
response.setContentType("text/html; Charset="+ t.g etEncoding ());
Writer out=response. GetWriter ();

Try {
T.p rocess (root, out, ObjectWrapper BEANS_WRAPPER);

} the catch (TemplateException e) {
Throw new ServletException (" process template wrong ", e);
} the finally {
If (out!=null) {
out.close();
}
out.flush();
}
}

}


Template code


<body>
<# list of foods as food>
The ${food. Id}
${food. ChineseName}




Error message

A FreeMarker template error:
The following has evaluated The to null or missing:
==& gt; Foods [in the template "index. The FTL" at line 3, the column 16]
-
Tip: If the failing expression is known to be legally refer to something that 's sometimes null or missing, either specify a default value like myOptionalVar! MyDefault, or the use & lt; # if myOptionalVar??> The when - present<# else> The when - missing . (These only cover the last step of the expression; To cover the whole expression, use parenthesis: (myOptionalVar. Foo)! myDefault, (myOptionalVar.foo)??
-
-
FTL stack trace (" - "means nesting - related) :
- Failed at: # list foods as food [in the template "index. The FTL" at line 3, the column 9]
-
For programmers with a Java stack trace () :
-
Freemarker. Core. InvalidReferenceException: [... the Exception message was already printed; see it above...
At a freemarker. Core. InvalidReferenceException. GetInstance (InvalidReferenceException. Java: 131)
At a freemarker. Core. Expression. AssertNonNull (Expression. Java: 194)
At a freemarker. Core. IteratorBlock. AcceptWithResult (IteratorBlock. Java: 74)
At a freemarker. Core. IteratorBlock. Accept (IteratorBlock. Java: 64)
At a freemarker. Core. Environment. Visit (324) Environment. Java:
At a freemarker. Core. MixedContent. Accept (MixedContent. Java: 54)
At a freemarker. Core. Environment. Visit (324) Environment. Java:
At a freemarker. Core. The Environment. The process (302) Environment. Java:
. At a freemarker template. The template. The process (325) template. Java:
At com. Servlet. IndexServlet. Service (IndexServlet. Java: 67)
The at javax.mail. Servlet. HTTP. HttpServlet. Service (HttpServlet. Java: 731)
The at org. Apache. Catalina. Core. ApplicationFilterChain. InternalDoFilter (ApplicationFilterChain. Java: 303)
The at org. Apache. Catalina. Core. ApplicationFilterChain. DoFilter (ApplicationFilterChain. Java: 208)
At org, apache tomcat, websocket server WsFilter. DoFilter (WsFilter. Java: 52)
The at org. Apache. Catalina. Core. ApplicationFilterChain. InternalDoFilter (ApplicationFilterChain. Java: 241)
The at org. Apache. Catalina. Core. ApplicationFilterChain. DoFilter (ApplicationFilterChain. Java: 208)
The at org. Apache. Catalina. Core. StandardWrapperValve. Invoke (StandardWrapperValve. Java: 218)
The at org. Apache. Catalina. Core. StandardContextValve. Invoke (StandardContextValve. Java: 110)
The at org. Apache. Catalina. The authenticator. AuthenticatorBase. Invoke (AuthenticatorBase. Java: 506)
The at org. Apache. Catalina. Core. StandardHostValve. Invoke (StandardHostValve. Java: 169)
The at org. Apache. Catalina. Valves. ErrorReportValve. Invoke (ErrorReportValve. Java: 103)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related