| Author |
type casting error
|
thippe swamy
Greenhorn
Joined: Feb 04, 2012
Posts: 10
|
|
i have set hashmap object using
session.setAttribute("bc",booksCollection); in one servlet.
i am trying to access the bc in another servlet by using HashMap<String,String> hm = (HashMap<String,String>) session.getAttribute("bc");
i get the type casting error saying: can't typecast from cbject to HashMap.
how do i get rid of this.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 2686
|
|
|
Well, that's telling you that the attribute isn't actually a HashMap, which is why you get an error when you try and cast it. So the question is, what is bookCollection? You'll have to look at the code that puts the "bc" attribute into the session in the first place.
|
 |
thippe swamy
Greenhorn
Joined: Feb 04, 2012
Posts: 10
|
|
|
bookCollection is an HashMap. session.setAttribute actually returns a generic Object right? I have used bc in the jsp it works fine.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 2686
|
|
|
Well, that error is caused by trying to cast something to a HashMap that isn't a HashMap. My best guess would be that the "bc" attribute is also being set somewhere else, with a different type. Are you sure that's not happening?
|
 |
thippe swamy
Greenhorn
Joined: Feb 04, 2012
Posts: 10
|
|
|
I have set the bc in only one servlet and it is a HashMap type, and I am trying access in other servlet, same thing is happening with the ArrayList. I did supress warnings and tried to display hm both are giving me null.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
The error message says that the object you're getting is not a HashMap. So you are mistaken about something. To find out what, try this code:
Then you will see what the actual class of the object is.
|
 |
thippe swamy
Greenhorn
Joined: Feb 04, 2012
Posts: 10
|
|
|
Thank you both Matthew Brown and Paul Clapham for your help. bc is an actually HashMap and I was trying to use it before creating it, I was getting Null pointer exception.
|
 |
 |
|
|
subject: type casting error
|
|
|