Link to home
Create AccountLog in
Avatar of jaggernat
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=(HttpServletRequest)request.getSession();
   HttpSession session= request.getSession();
   System.out.println("CHECKER++++");  
     
  UserVO usrvo = (UserVO)session.getAttribute("userDetails");
  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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
if u need the request then it should be passed as a parameter:

public ArrayList getJuriIds(HttpServletRequest request)
{
   HttpSession session= request.getSession();
   System.out.println("CHECKER++++");  
     
  UserVO usrvo = (UserVO)session.getAttribute("userDetails");
  String groupId= usrvo.getGroupId();
  call DAO and get collection;

return arraylist;
}
ArrayList jurisdictionIds = getJuriIds(req);

pass that request object here as per object told
Avatar of jaggernat
jaggernat

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("userDetails");
 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=(HttpServletRequest)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.
>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
when i do something like this its giving me error


ArrayList jurisdictionIds = getJuriIds(HttpServletRequest request);
public ArrayList getJuriIds(request)

{
   HttpSession session= request.getSession();
   System.out.println("CHECKER++++");  
     
  UserVO usrvo = (UserVO)session.getAttribute("userDetails");
  String groupId= usrvo.getGroupId();
  call DAO and get collection;

return arraylist;
}
ASKER CERTIFIED SOLUTION
Link to home
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=(HttpServletRequest)request.getSession();

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?
thanks.works