Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

null pointer exception on HttpServletRequest

Posted on 2006-03-23
12
Medium Priority
?
1,033 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


0
Comment
Question by:jaggernat
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 800 total points
ID: 16276538
you cannot do that, HttpServletRequest are for use in servlets

what is it u are trying to do?
0
 
LVL 92

Expert Comment

by:objects
ID: 16276540
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
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16276754
ArrayList jurisdictionIds = getJuriIds(req);

pass that request object here as per object told
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Author Comment

by:jaggernat
ID: 16276900
>>>>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
 
LVL 92

Expert Comment

by:objects
ID: 16276934
then pass the request (or the session) as a parameter
0
 

Expert Comment

by:wcleung9
ID: 16277887
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
 

Expert Comment

by:wcleung9
ID: 16277896
>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
 
LVL 10

Author Comment

by:jaggernat
ID: 16279349
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 800 total points
ID: 16279793
>> ArrayList jurisdictionIds = getJuriIds(HttpServletRequest request);

would be ArrayList jurisdictionIds = getJuriIds ( request ) ;

>> public ArrayList getJuriIds(request)

would be public ArrayList getJuriIds ( HttpServletRequest request )

0
 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 16279863
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16279916
Its been corrected in the later code which was posted by jaggernat. Just a compilation error issue which I guess is fixed?
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16280314
thanks.works
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question