当前位置: 代码迷 >> Java相关 >> servlet获取session属性值,出现问指针错误的有关问题
  详细解决方案

servlet获取session属性值,出现问指针错误的有关问题

热度:6162   发布时间:2013-02-25 21:49:25.0
servlet获取session属性值,出现问指针异常的问题?
Exception report

message 

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

exception 

java.lang.NullPointerException
com.servlet.ServletB.doPost(ServletB.java:29)
com.servlet.ServletB.doGet(ServletB.java:22)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


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

代码出现的错误的地方:
HttpSession session=request.getSession();
System.out.println(session.getAttribute("name"));
String name=session.getAttribute("name").toString();
String sessionid=session.getAttribute("sessionID").toString(); response.setContentType("text/html;charset=gbk");
response.setCharacterEncoding("gbk");
PrintWriter out=response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");//需要导入的JSP相关代码
out.println("<html>");
out.println("<head><title>获取session的相关属性</titel>");
out.println("</head>");
out.println("<body>");
out.println("session的name为:"+name);
out.println("session的sessionid为:"+sessionid);
out.println("</body>");
out.println("</html>");
out.flush();
out.close();

------解决方案--------------------------------------------------------
String sessionid=session.getAttribute("sessionID").toString(); 
改成:
String sessionid=(String)session.getAttribute("sessionID");
  相关解决方案