We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

null pointer exception on HttpServletRequest

jaggernat
jaggernat asked
on
Medium Priority
2,017 Views
Last Modified: 2007-12-19
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


Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
you cannot do that, HttpServletRequest are for use in servlets

what is it u are trying to do?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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;
}
Manikandan ThiagarajanSenior consultant

Commented:
ArrayList jurisdictionIds = getJuriIds(req);

pass that request object here as per object told

Author

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.



Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
then pass the request (or the session) as a parameter

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

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

Author

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;
}
Principal Technologist
CERTIFIED EXPERT
Commented:
>> ArrayList jurisdictionIds = getJuriIds(HttpServletRequest request);

would be ArrayList jurisdictionIds = getJuriIds ( request ) ;

>> public ArrayList getJuriIds(request)

would be public ArrayList getJuriIds ( HttpServletRequest request )

Leo EikelmanDirector, IT and Business Development

Commented:
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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Its been corrected in the later code which was posted by jaggernat. Just a compilation error issue which I guess is fixed?

Author

Commented:
thanks.works
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.