所以这就是事情。我尝试使用 jsp 在网站主页上显示一些消息列表。但是当tomcat服务器启动时,我只看到这个错误“HTTP Status 500 - java.lang.NullPointerException”,它指向“for(Message message:messageList)”这一行
这是servlet的代码:
public class MainPageServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MessageManager manager = new MessageManager();
List<Message> messageList = manager.getMessages();
request.setAttribute("messages", messageList);
request.getRequestDispatcher("/main-page.jsp").include(request, response); } }
这是jsp的一部分:
<%
List<Message> messageList = (List<Message>) request.getAttribute("messages");
for (Message message : messageList) {
%>
<div class="row" style="margin-top: 15px">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
<%= message.getLogin()%>
</h3>
</div>
<div class="panel-body">
<%= message.getMessage() %>
</div>
<div class="panel-footer">
Posted at: <%= message.getDate() %>
</div>
</div>
</div>
</div>
<%
}
%>
我已经测试了获取 messageList 的结果,它是 100% 好的。那么漏洞在哪里呢?提前感谢您的帮助!