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.
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(JspSe开发者_如何学编程rvletWrapper.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:
- The
session
isnull
andgetAttribute()
will fail with NPE. - The attribute "professor" returned
null
and theint
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.
精彩评论