jaggernat
asked on
null pointer exception on HttpServletRequest
hi experts ,
I have a java file and i have a method in it.
it goes like this
public class xyz
{
ArrayList jurisdictionIds = getJuriIds();
public ArrayList getJuriIds()
{
String[] juriIds = new String[1000];
HttpServletRequest request= null;
request=(HttpServletReques t)request. getSession ();
HttpSession session= request.getSession();
System.out.println("CHECKE R++++");
UserVO usrvo = (UserVO)session.getAttribu te("userDe tails");
String groupId= usrvo.getGroupId();
call DAO and get collection;
return arraylist;
}
}
The problem is I am creating a session object above, but its throwing a null pointer exception when this method is called.
I am assuming its because i am giving HttpServletRequest request= null;
any ideas how i can correct this problem.
I cannot do HttpServletRequest req= new HttpServletRequest (); because it says HttpServletRequest cannot be instantiated.
any help greatly appreciated,
thanks
J
I have a java file and i have a method in it.
it goes like this
public class xyz
{
ArrayList jurisdictionIds = getJuriIds();
public ArrayList getJuriIds()
{
String[] juriIds = new String[1000];
HttpServletRequest request= null;
request=(HttpServletReques
HttpSession session= request.getSession();
System.out.println("CHECKE
UserVO usrvo = (UserVO)session.getAttribu
String groupId= usrvo.getGroupId();
call DAO and get collection;
return arraylist;
}
}
The problem is I am creating a session object above, but its throwing a null pointer exception when this method is called.
I am assuming its because i am giving HttpServletRequest request= null;
any ideas how i can correct this problem.
I cannot do HttpServletRequest req= new HttpServletRequest (); because it says HttpServletRequest cannot be instantiated.
any help greatly appreciated,
thanks
J
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ArrayList jurisdictionIds = getJuriIds(req);
pass that request object here as per object told
pass that request object here as per object told
ASKER
>>>>what is it u are trying to do?
just trying to create a session in java class because i want to get the object 'userDetails' (session.getAttribute("use rDetails") ;
which is in session.
just trying to create a session in java class because i want to get the object 'userDetails' (session.getAttribute("use
which is in session.
then pass the request (or the session) as a parameter
Except the nature of HttpServletRequest and HttpSession classes, I would like to point out some coding error:
First you set the request pointer to be null:
HttpServletRequest request= null;
Then you call its method getSession():
request=(HttpServletReques t)request. getSession ();
This will definitely induce a NullPointerException! The request pointer is not pointing to any object. You just set it to null yourselves.
And, even if the request pointer is pointing to a object, this line of code will induce a ClassCastException. Calling request.getSession() will return a HttpSession object. You can't cast it to a HttpServletRequest object. In Java, you can only cast an object to its parent class, or the interface it implements.
First you set the request pointer to be null:
HttpServletRequest request= null;
Then you call its method getSession():
request=(HttpServletReques
This will definitely induce a NullPointerException! The request pointer is not pointing to any object. You just set it to null yourselves.
And, even if the request pointer is pointing to a object, this line of code will induce a ClassCastException. Calling request.getSession() will return a HttpSession object. You can't cast it to a HttpServletRequest object. In Java, you can only cast an object to its parent class, or the interface it implements.
>In Java, you can only cast an object to its parent class, or the interface it implements.
Sorry, it should be:
you can only cast an object to its sub class
Sorry, it should be:
you can only cast an object to its sub class
ASKER
when i do something like this its giving me error
ArrayList jurisdictionIds = getJuriIds(HttpServletRequ est request);
public ArrayList getJuriIds(request)
{
HttpSession session= request.getSession();
System.out.println("CHECKE R++++");
UserVO usrvo = (UserVO)session.getAttribu te("userDe tails");
String groupId= usrvo.getGroupId();
call DAO and get collection;
return arraylist;
}
ArrayList jurisdictionIds = getJuriIds(HttpServletRequ
public ArrayList getJuriIds(request)
{
HttpSession session= request.getSession();
System.out.println("CHECKE
UserVO usrvo = (UserVO)session.getAttribu
String groupId= usrvo.getGroupId();
call DAO and get collection;
return arraylist;
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
From what context are you calling this method?
Because the problem is in this line
>>request=(HttpServletRequ est)reques t.getSessi on();
returning null, which might mean you are calling it from the wrong context because it can not find a session
Leo
Because the problem is in this line
>>request=(HttpServletRequ
returning null, which might mean you are calling it from the wrong context because it can not find a session
Leo
Its been corrected in the later code which was posted by jaggernat. Just a compilation error issue which I guess is fixed?
ASKER
thanks.works
public ArrayList getJuriIds(HttpServletRequ
{
HttpSession session= request.getSession();
System.out.println("CHECKE
UserVO usrvo = (UserVO)session.getAttribu
String groupId= usrvo.getGroupId();
call DAO and get collection;
return arraylist;
}