• 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

error when try to get a object from TreeMap

 
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
below is my code



i am getting exception line " AllowanceDTO x=map.get(data);"

why am getting exception in that line?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exception do you get? What type does the Map use for Keys? What type does the Map use for Values? What is the parameter the Map expects in the get() method? What type are you giving as the parameter to the get() method?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the map.get(...) method, the argument is the key. Which means that it looks like you're trying to use an AllowanceDTO object as the key, whereas in fact it's the value.

Now, if you'd declared your map variable properly - e.g. as a Map<String, AllowanceDTO> - then that would give you a compile time error rather than an exception. But if you declared it as a plain Map then the compiler wouldn't protect you like that. You haven't shown us where it is declared.

If you've declared it as a plain Map (or TreeMap), the error is probably because it's trying to cast your AllowanceDTO object to a Comparable - because that's how TreeMaps work - but it probably isn't one.
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic