Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error when try to get a object from TreeMap

 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • 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?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic