Java – Tomcat and null pointer exceptions when accessing session properties

I have been developing a project using net beans IDE for 6 months to develop an e-learning web application Everything works well in net beans (the project comes from existing resources, so I have to modify it. I didn't develop the whole application. I use Apache Tomcat in net beans. 7. When I create a war file and deploy it, it doesn't work. I get null pointer exceptions in my session variables, as if I never gave them a value. I can't understand what the problem is. Iinside net beans I use 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 on most of my pages Strangely, some session variables are not empty I can't understand where to focus!

thank you.

//Editor: solved! The problem is that in the source I got, they forgot the * in the WEB-INF folder Class file, so when I clean up and build in NetBeans, the new class is not compiled, and the net bean uses the previous source from the WEB-INF folder When I manually delete all the files in Web - inf / classes Class file, the new file is used in the next clean build

Solution

Therefore, line 31 reads as follows:

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

There are only two technical reasons for NPE here:

>Session is null, getattribute() will fail due to NPE. > The property "Professor" returns null and < = int auto@R_623_2419 @Ing will fail due to NPE The first reason is unlikely because line 30 will fail first I bet that's the second reason You need to check whether the property is null

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

Maybe you need to solve it at a higher level, that is, to ensure that the "Professor" attribue is never empty

It has nothing to do with specific problems. Java code belongs in Java classes It will make debugging, testing and maintenance easier

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>