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


LVL 10
jaggernatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
you cannot do that, HttpServletRequest are for use in servlets

what is it u are trying to do?
0
objectsCommented:
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;
}
0
Manikandan ThiagarajanSenior consultantCommented:
ArrayList jurisdictionIds = getJuriIds(req);

pass that request object here as per object told
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

jaggernatAuthor Commented:
>>>>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.



0
objectsCommented:
then pass the request (or the session) as a parameter
0
wcleung9Commented:
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.
0
wcleung9Commented:
>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
0
jaggernatAuthor Commented:
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;
}
0
Mayank SAssociate Director - Product EngineeringCommented:
>> ArrayList jurisdictionIds = getJuriIds(HttpServletRequest request);

would be ArrayList jurisdictionIds = getJuriIds ( request ) ;

>> public ArrayList getJuriIds(request)

would be public ArrayList getJuriIds ( HttpServletRequest request )

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leo EikelmanDirector, IT and Business DevelopmentCommented:
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
0
Mayank SAssociate Director - Product EngineeringCommented:
Its been corrected in the later code which was posted by jaggernat. Just a compilation error issue which I guess is fixed?
0
jaggernatAuthor Commented:
thanks.works
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.