• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Check session on a Java code

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In my system, I'm trying to check the session status via a java method. But it gave me an NullPointerException. So I've reduced some code to find the error but I couldn't able to find it. Further the same code with some alteration will work perfectly in JSP page.

My JSP Page


My SessionCheck Java Class


This is the error

HTTP Status 500 - An exception occurred processing JSP page /newjsp.jsp at line 22

type Exception report

message An exception occurred processing JSP page /newjsp.jsp at line 22

description The server encountered an internal error that prevented it from fulfilling this request.

exception

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

19: <%
20: session.setAttribute("status", "Test");
21: SessionCheck sc = new SessionCheck();
22: boolean sesVal = sc.sesCheck();
23:
24: %>
25: </body>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

java.lang.NullPointerException
common.SessionCheck.sesCheck(SessionCheck.java:12)
org.apache.jsp.newjsp_jsp._jspService(newjsp_jsp.java:82)
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:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.30 logs.

Apache Tomcat/7.0.30



Can anyone help me out this issue?
 
Ranch Hand
Posts: 75
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

The cause of the NullPointerException is clearly stated in the stacktrace:

Sumer Selvaraj wrote:
java.lang.NullPointerException
common.SessionCheck.sesCheck(SessionCheck.java:12)



Now, if you check line 12 in your SessionCheck class, what's the value of your session variable ?

Claudiu
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

HttpSession session;

public boolean sesCheck() {
String sesValue = (String) session.getAttribute("status");



How does the session object passed to this class?
 
Sumer Selvaraj
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Claudiu Chelemen wrote:Hello

The cause of the NullPointerException is clearly stated in the stacktrace:

Sumer Selvaraj wrote:
java.lang.NullPointerException
common.SessionCheck.sesCheck(SessionCheck.java:12)



Now, if you check line 12 in your SessionCheck class, what's the value of your session variable ?

Claudiu



I've passed the session variable in JSP page as following code


Further to check the the value I've removed the session.setAttribute() from the JSP page and added it to the class file


Now I'm getting error as the same.
 
Sumer Selvaraj
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bala nannaka wrote:

HttpSession session;

public boolean sesCheck() {
String sesValue = (String) session.getAttribute("status");



How does the session object passed to this class?



I'm so sorry as I couldn't able to get your question. I'm a novice and developing an application for my final year degree project.
 
bala nannaka
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionCheck {

HttpServletRequest request;
HttpSession session;

public boolean sesCheck() {
session.setAttribute("status","test");
String sesValue = (String) session.getAttribute("status");
if (sesValue.equals("active"))
return true;
return false;
}
}



SessionCheck is a simple plain old java class .HttpSession will be not available for this class untill you pass it externally .

I would recomemnd you to pass it as a method parameter



and in jsp you pass it as a parameter
 
Sumer Selvaraj
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bala nannaka wrote:


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionCheck {

HttpServletRequest request;
HttpSession session;

public boolean sesCheck() {
session.setAttribute("status","test");
String sesValue = (String) session.getAttribute("status");
if (sesValue.equals("active"))
return true;
return false;
}
}



SessionCheck is a simple plain old java class .HttpSession will be not available for this class untill you pass it externally .

I would recomemnd you to pass it as a method parameter



and in jsp you pass it as a parameter



Thank you so much. It worked perfectly without any issues.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic