• 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

getting a class cast exception

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, I create an ArrayList:

List agentList = new ArrayList();

Then I put some stuff into it. Then I try the following:

SortedSet agentSet = new TreeSet();
agentSet.addAll(agentList);

What should I do to avoid getting a ClassCastException?

Thanks for any help.

Dorcas
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can you please provide a complete source code snippet ? What are you putting in the ArrayList exactly ?

Regards ...
 
Dorcas Rebanha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummm, the agentList contains AgentDisplayObjects, and I don't know more than that. Somebody else wrote the code and I am just trying to understand it, and possibly fix that pesky ClassCastException.

Are you suggesting that the contents of agentList might the problem?

I originally thought simply that you couldn't addAll() an ArrayList into a TreeSet. So I was hoping for advice about getting around that, like maybe a different method to use to accomplish the purpose.

Dorcas
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Please post a snippet of your code.

Thanks ...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TreeSet requires that the objects that you put into it implement interface Comparable.

You get this exception because your class AgentDisplayObject does not implement Comparable.

Note, if you can't modify class AgentDisplayObject, then you can use the constructor of class TreeSet that takes a Comparator as an argument (see the API documentation of class TreeSet). When you do that, your class AgentDisplayObject does not need to implement Comparable - the comparator that you pass to the constructor will be responsible for sorting AgentDisplayObject objects instead.
 
Dorcas Rebanha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper! Thank you! You rock!

Dorcas
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad I could help...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic