使用ServletContext时的java.lang.NullPointerException

时间:2023-01-19 21:40:16

I was examining the use of ServletContext when i got the null pointer exception. I don't understand why do i get this exception.

当我得到空指针异常时,我正在研究ServletContext的使用。我不明白为什么我会得到这个例外。

I have set the attribute in the context object from one class and then try to retrieve that from the second class using getAttribute(...).

我在一个类中设置了上下文对象中的属性,然后尝试使用getAttribute(...)从第二个类中检索该属性。

package ServletContext; // servlet1

import javax.servlet.*; 
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class servlet1 extends HttpServlet {
  public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
    LinkedList list = new LinkedList();
    list.add("suhail");
    ServletContext servletContext = getServletContext();
    servletContext.setAttribute("name", list);
  }
}

package ServletContext;  // servlet2

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class servlet2 extends HttpServlet {
 public void doGet(HttpServletRequest request , HttpServletResponse response) throws IOException,ServletException {
    LinkedList list2 = new LinkedList();
    ServletContext context = getServletContext();
    list2 = (LinkedList)context.getAttribute("name");
    PrintWriter writer = response.getWriter();
    response.setContentType("text/plain");
    writer.println(list2.pop()); //**15th statement**
  }
}

Exception is :

例外情况是:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
ServletContext.servlet2.doGet(servlet2.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.11 logs.

note备注Apache Tomcat / 7.0.11日志中提供了根本原因的完整堆栈跟踪。

Why am i getting this exception ? list should be initialized in servlet2

为什么我得到这个例外? list应该在servlet2中初始化

1 个解决方案

#1


0  

list2 is null there - you can't invoke methods on null. Perhaps you still haven't put the list in the servlet context? A null-check (if (list != null)) would fix the exception, but make sure you are properly putting the list in the context before you invoke the 2nd servlet.

list2在那里为null - 你不能在null上调用方法。也许您还没有将列表放在servlet上下文中?空检查(if(list!= null))将修复异常,但请确保在调用第二个servlet之前正确地将列表放在上下文中。

#1


0  

list2 is null there - you can't invoke methods on null. Perhaps you still haven't put the list in the servlet context? A null-check (if (list != null)) would fix the exception, but make sure you are properly putting the list in the context before you invoke the 2nd servlet.

list2在那里为null - 你不能在null上调用方法。也许您还没有将列表放在servlet上下文中?空检查(if(list!= null))将修复异常,但请确保在调用第二个servlet之前正确地将列表放在上下文中。