日期:2014-05-20  浏览次数:20864 次

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");
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html