wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problems with Java.lang.ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with Java.lang.ClassCastException" Watch "Problems with Java.lang.ClassCastException" New topic
Author

Problems with Java.lang.ClassCastException

Katy Parsons
Greenhorn

Joined: Mar 30, 2009
Posts: 2
I'm trying to create a poker game for my CS project but I keep getting a java.lang.ClassCastException error and I don't know where it's coming from....

This is the game class, which I believe is where the error is coming from:


And here is the error that is showing up:
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The stack trace shows you that the problem is here at Game.checkRoyalFlush(Game.java:294).
It also shows you that you are trying to sort an array whose element does not implements the Comparable interface. Check the sort() method of the java.util.Arrays class for more details.



slots is of type GVstack[], so the question is : Does GVstack implements Comparable ?

[My Blog]
All roads lead to JavaRanch
Katy Parsons
Greenhorn

Joined: Mar 30, 2009
Posts: 2
So, what can I do so that my array elements implement the comparable interface? Our Professor just told us to use the sort method for arrays and didn't go into any explanation.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

So, what can I do so that my array elements implement the comparable interface?


Well, you can actually "implement the java.lang.Comparable interface"? The purpose of the interface is to provide a method that allows your element to be compared to another element -- this is needed, because it is not possible to sort an array, if the sort algorithm can't tell which element comes first.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
GeeCON Prague 2014
 
subject: Problems with Java.lang.ClassCastException