• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

casting problem

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all I have the below casting problem in my servlet program for stock trading system:
"java.lang.ClassCastException: trade.equity.XXXEquity cannot be cast to java.lang.Comparable"

The XXXEquity is a subclass of Equity, code looks like:



In servlet program parse URL content and add content to some set call it equitySet. In servlet I did something like:


equityBean class has addEquity method that looks like:


I don't know whether it is the compareTo method inside compare method of Equity class or simply with the line

in my servlet or something else that is causing the error.
Also do I really need to implement the Comparator interface in XXXEquity?

Please help.
[ July 11, 2008: Message edited by: K. Tsang ]
 
Marshal
Posts: 76415
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message with your Exception tells you what you need to know.

Whichever object you have as equityTicker (BTW: why are you using direct access to a field?) does not implement the Comparable<T> interface and therefore does not have a compareTo(T t) method.
You might have a spelling error that you are implementing Comparator instead of Comparable<T>.

I cannot see an explicit cast operator in any of the code you have quoted, so you have probably not yet found the line where the exception took place.

BTW: Did you post your code with ctrl-c ctrl-v? There is a misprint in the fully-qualified name of Comparator, which would cause a compiler error.
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed the problem by changing the Comparator to Comparable.

I also wonder why Comparator wouldn't work for sets? since both return an int. Does anyone know if the Set interface implements the Comparable interface inside its code that no one know about?
 
Campbell Ritchie
Marshal
Posts: 76415
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sets don't have a "natural ordering," so there is no point in them implementing Comparable.

And there is nothing implemented inside an interface, so no they don't. Interface methods are always implemented as public, anyway.
 
reply
    Bookmark Topic Watch Topic
  • New Topic