Hey all, I am writing a poker game, with not much luck!
As far as hand evaluating goes, I decided not to use some sort of numerical evaluating system to calculate the higher values of hands. Instead, I created a different class for all of the hand ( FourOfAKind, TwoPair, etc etc ).
Each of them extends another class called Hand.
So anyways, I get this NPE sometimes and I cant exactly pinpoint the cause of it.
Exception in thread "AWT-EventQueue-0"
java.lang.NullPointerException
at OnePair.compareTo(OnePair.java:90)
at Hand.compareTo(Hand.java:54)
at HandEvaluator.determineHand(HandEvaluator.java:97)
at HandEvaluator.evaluate(HandEvaluator.java:43)
at HandEvaluator.<init>(HandEvaluator.java:27)
at Player.getBestHand(Player.java:50)
at Dealer.updateHands(Dealer.java:96)
at Dealer.doAction(Dealer.java:70)
at GameManager.playHand(GameManager.java:42)
Since there is wayy to much code to post it all, I'll post a few pieces from the lines refferred to in the output above.
//at OnePair.compareTo(OnePair.java:90)
public int compareTo( OnePair op )
{
Value thisPair = getPairValue();
Value opPair = op.getPairValue();
if( thisPair.compareTo( opPair ) > 0 )
return 1;
else if( thisPair.compareTo( opPair) < 0 )
return -1;
else // check the high card
}
The error is occuring at
if( thisPair.compareTo( opPair ) > 0 )
This implies that thisPair == null, which implies that getPairValue() has something wrong.
public Value getPairValue ()
{
for( Card c: hand )
{
Value pair = c.getValue ();
int count = 0;
// Compare the current cards value to the rest of the hand
// to check for another card of the same value.
for( Card d: hand )
if( d.getValue ().equals (pair))
count ++;
if( count == 2 ) return pair;
}
return null;
}
In order for getPairValue() to return null, it implies that none of the cards in the 'hand' which is an ArrayList< Card > of length (5) always form a pair.
So, this implies that this Hand should not be of the type OnePair at all!.. which implies that there is something wrong elsewhere in determining if a hand is a OnePair, TwoPair, etc.
So... this leads to where I determine if a given set of cards contains one pair, in the HandEvaluator class..
private boolean isOnePair ( ArrayList< Card > cards )
{
for( Card c: cards )
{
Value pair = c.getValue ();
int count = 0;
for( Card d: cards )
if( d.getValue ().equals ( pair )) count ++;
if( count == 2 ) return true;
}
return false;
}
So, when determining what kind of hand a particular group of cards forms, i use this isOnePair() method. If it is a pair, then it should be safe to construct a new OnePair object.
if( isRoyalFlush( cards ) )
....
else if ( is TwoPair( cards ) )
...
else if( isOnePair ( cards ))
return new OnePair (cards);
Back to the original problem, I cant figure out why getPairValue() in the OnePair class is getting to the 'return null' line. Since I already checked whether the hand had a pair before even constructing the OnePair object, this return null case should never occur.
Oh yea. Value is just an enum
public enum Value
{
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING;
}
Any ideas?