简体   繁体   English

访问会话属性时Tomcat和空指针异常

[英]Tomcat and null pointer exception when accesing session attributes

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. 我一直在使用net beans ide开发一个项目6个月,以便开发一个电子学习web应用程序。所有东西在net beans中运行良好。(该项目来自现有的资源,我不得不修改它,我没有开发整个应用程序我在net beans中使用apache tomcat 7.当我创建war文件并部署它没什么工作。我在我的会话变量中得到空指针异常,就像我从来没有给它们一个值。我无法理解是什么问题.Iinside net beans我使用相同的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 //编辑:解决了!问题是在我得到的来源中他们忘记了WEB-INF文件夹中的* .class文件所以当我在NetBeans中清理和构建时,新类没有编译,而net bean使用了以前的来自WEB-INF文件夹的源。当我手动删除WEB-INF / Classes中的所有.class文件时,在下一个clean-build中使用了新文件

org.apache.jasper.JasperException: An exception occurred processing JSP page /System.jsp at line 31 org.apache.jasper.JasperException:在第31行处理JSP页面/System.jsp时发生异常

Line 31 is thus the following: 因此,第31行如下:

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

A NPE here can technically have only 2 causes: 这里的NPE在技术上只能有两个原因:

  1. The session is null and getAttribute() will fail with NPE. sessionnullgetAttribute()将因NPE而失败。
  2. The attribute "professor" returned null and the int autoboxing for <= will fail with NPE. 属性“professor”返回null ,而<=int autoboxing将因NPE而失败。

The first cause is unlikely as line 30 would otherwise have failed first. 第一个原因不太可能,因为第30行将首先失败。 I'll bet that it's the 2nd cause. 我敢打赌,这是第二个原因。 You need to check first if the attribute is not null . 您需要先检查该属性是否为null

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

Perhaps you need to solve it at higher level, ie make sure that the "professor" attribtue is never null . 也许你需要在更高层次上解决它,即确保“教授”attribtue 永远不会为 null


Unrelated to the concrete problem, Java code belongs in Java classes . 具体问题无关Java代码属于Java类 It'll make debugging, testing and maintainance and such much easier. 它将使调试,测试和维护变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM