HTTP 状态 500 - java.lang.NullPointerException Servlet + jsp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19347563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:20:22  来源:igfitidea点击:

HTTP Status 500 - java.lang.NullPointerException Servlet + jsp

javajsptomcatservlets

提问by user2855400

So here is the thing. I try to display some list of messages on main page of the web-site by using jsp. But when the tomcat server starts, I'm watching only on this error "HTTP Status 500 - java.lang.NullPointerException" that points to the line "for (Message message : messageList)"

所以这就是事情。我尝试使用 jsp 在网站的主页上显示一些消息列表。但是当 tomcat 服务器启动时,我只看到这个错误“HTTP 状态 500 - java.lang.NullPointerException”指向“for (Message message : messageList)”行

Here is the code of servlet:

这是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); } }

And here is a part of jsp:

这是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>
<%
}
%>

I've tested on result of getting messageList and it's 100% ok. So where is the bug? Thnx in advance for helping!

我已经对获取 messageList 的结果进行了测试,它 100% 没问题。那么错误在哪里呢?提前感谢您的帮助!

回答by Vinoth Krishnan

In your scriptlet first check your messages is not null. Like,

在您的脚本中,首先检查您的消息是否为空。喜欢,

if (request.getAttribute("messages") != null){
   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>
<%
  }
}
 %>

So you may not come across null pointer exception. If you are not getting any data via request use session to send the values across the page. Try and let me know..

所以你可能不会遇到空指针异常。如果您没有通过请求获取任何数据,请使用 session 跨页面发送值。试着让我知道..

回答by 3bu1

List<Message> messageList = (List<Message>) request.getAttribute("messages"); 

here you didn't type cast..

在这里你没有打字..

<%MainPageServlet  messageList = (MainPageServlet) request.getAttribute("messages"); %>

try this,it may help you

试试这个,它可能会帮助你

NOTE: NOT TESTED

注意:未测试