• Post Reply Bookmark Topic Watch Topic
  • New Topic

type casting error  RSS feed

 
thippe swamy
Greenhorn
Posts: 10
Chrome Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Chrome Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Chrome Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Chrome Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!