Tomcat和空指针异常accesing会话属性时 [英] Tomcat and null pointer exception when accesing session attributes

查看:201
本文介绍了Tomcat和空指针异常accesing会话属性时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为以发展电子学习网络application.Everything工作的罚款净豆内使用净豆的IDE 6个月的项目(该项目是从现有来源,我不得不修改它,我没有发展整个应用程序),我使用Apache Tomcat 7的净beans.When我创建war文件并部署它没有worked.I我在我的会话变量获得空指针异常就像我从来没有给他们一个value.I我不能够理解什么是problem.Inside净豆我使用的是相同的Tomcat。

  org.apache.jasper.JasperException:发生处理JSP页面/System.jsp例外在第31行28:整数intObj =新的整数(PROJECT_ID);
29:httpsession.setAttribute(PROJECT_ID,intObj);
30:Hashtable的项目=(哈希表)session.getAttribute(projectsprofessor);
31:如果((整数)session.getAttribute(教授)< = 1&安培;&安培;
32:projects.get(PROJECT_ID)== NULL)
33:{
34:request.getSession()的setAttribute(的errorMessage,!这个项目属于另一位教授);
堆栈跟踪:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)根本原因显示java.lang.NullPointerException
    org.apache.jsp.System_jsp._jspService(System_jsp.java:149)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这恰好是最我pages.The奇怪的是,一些会话变量不为空。
我不明白的地方重点!

感谢您。

//编辑:!解决的问题是,在源代码,我得到他们已经忘记了在* WEB-INF文件夹中的.class文件,所以当我清理和建立在NetBeans中新的类没有编译和豆网使用从WEB-INF folder.When我手动删除从WEB-INF / classes中所有的.class文件在未来的清洁构建使用了新的文件previous源


解决方案

  

org.apache.jasper.JasperException:发生处理JSP页面/System.jsp例外在第31行


31号线因此以下内容:

  IF((整数)session.getAttribute(教授)< = 1&安培;&安培; ...

这里的一个NPE可以在技术上只有2个原因:


  1. 会议的getAttribute()将失败,NPE。

  2. 属性教授返回 INT 自动装箱为< = 将失败,NPE。

第一个原因是不太可能,因为30行否则将先失败了。我敢打赌,这是第二个原因。你需要先检查一下,如果属性不是

 整数教授= session.getAttribute(教授);
如果((教授== NULL ||教授< = 1)及和放大器; ...

也许你需要解决它在较高的水平,即确保了教授的attribtue是的从不


无关的具体问题,的 Java的code属于Java类的。这会让调试,检测和保养,这样更加容易。

I have been working on a project for 6 months using net beans ide in order to develop an elearning web application.Everything worked fine inside net beans.(The project was from existing sources and i had to modify it,i didnt develop the entire application)I was using apache tomcat 7 in net beans.When i created the war file and deployed it nothing worked.I am getting null pointer exceptions in my session variables like i never give them a value.I am not able to understand what's the problem.Inside net beans i am using the same tomcat.

    org.apache.jasper.JasperException: An exception occurred processing JSP page /System.jsp at line 31

28:       Integer intObj = new Integer(project_id);
29:       httpsession.setAttribute("project_id",intObj);
30:       Hashtable projects=(Hashtable)session.getAttribute("projectsprofessor");
31:       if((Integer)session.getAttribute("professor")<=1 &&
32:           projects.get(project_id)==null)
33:                    {
34:           request.getSession().setAttribute("errorMessage","This project belongs to another professor!");


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.NullPointerException
    org.apache.jsp.System_jsp._jspService(System_jsp.java:149)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

This happens to the most of my pages.The strange is that some session variables are not null. I cannot understand where to focus!

thank you.

//Edit:Solved!The problem was that In the sources i got they have forgot *.class files in WEB-INF folder so when i clean-and-build in NetBeans the new classes didn't compile and net beans used the previous sources from the WEB-INF folder.When i deleted manually all the .class files from WEB-INF/Classes in the next clean-build used the new files

解决方案

org.apache.jasper.JasperException: An exception occurred processing JSP page /System.jsp at line 31

Line 31 is thus the following:

if((Integer)session.getAttribute("professor")<=1 && ...

A NPE here can technically have only 2 causes:

  1. The session is null and getAttribute() will fail with NPE.
  2. The attribute "professor" returned null and the int autoboxing for <= will fail with NPE.

The first cause is unlikely as line 30 would otherwise have failed first. I'll bet that it's the 2nd cause. You need to check first if the attribute is not null.

Integer professor = session.getAttribute("professor");
if ((professor == null || professor <= 1) && ...

Perhaps you need to solve it at higher level, i.e. make sure that the "professor" attribtue is never null.


Unrelated to the concrete problem, Java code belongs in Java classes. It'll make debugging, testing and maintainance and such much easier.

这篇关于Tomcat和空指针异常accesing会话属性时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆