• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

type casting error

 
Greenhorn
Posts: 10
Eclipse IDE Tomcat Server Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Tomcat Server Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Tomcat Server Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Tomcat Server Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic