im writing a poker program for my class and i keep getting this error that says
Exception in thread "main" java.lang.NullPointerException
at pokerPlayer.takeCard(pokerPlayer.java:40)
at aiPokerPlayer.takeCard(aiPokerPlayer.java:2)
at pokerDriver.main(pokerDriver.java:39)
im not quite sure how to fix this.
Thanks for the helpCode:/**
* @(#)pokerDriver.java
*
* pokerAI application
*
* @author CS301
* @version 0.93 2009/2/5
* This is a starter driver app to test your pokerPlayer classes
* 2/5. Correction to draw new cards for loop
* Change human first printVisible to printAll
* add section comments
* added aiDownCards, humDownCards & related code
*/
public class pokerDriver {
public static void main(String[] args) {
// TODO, add your application code
System.out.println("Hello Poker!");
Deck aDeck = new Deck(false);
aDeck.shuffle();
aiPokerPlayer ai = new aiPokerPlayer(50, "HAL");
humanPokerPlayer hum = new humanPokerPlayer(50, "ME");
Card aCard; // deal to players
for ( int i=1; i < 10; i++) {
aDeck.shuffle();
Hand aiDownCards = new Hand();
Hand humDownCards = new Hand();
for (int c = 1; c <= 5; c++){
aCard = aDeck.dealCard();
ai.takeCard(aCard);
if ( c <= 2) aiDownCards.addCard(aCard);
aCard = aDeck.dealCard();
hum.takeCard(aCard);
if ( c <= 2) humDownCards.addCard(aCard);
}
System.out.println("AI AI AI AI ");
ai.printVisible();
System.out.println("ONLY HUMAN");
hum.printAll();
// ai discards; dealers deals nc new cards
int nc = ai.discard( humDownCards);
System.out.println(" AI discards " + nc);
for (int c = 1; c <= nc; c++)
ai.takeCard( aDeck.dealCard());
// human discards; dealers deals nc new cards
nc = hum.discard(aiDownCards);
System.out.println(" Human discards " + nc);
for (int c = 1; c <= nc; c++)
hum.takeCard( aDeck.dealCard());
// See who won
System.out.println("AI AI AI AI ");
ai.printAll();
System.out.println("ONLY HUMAN");
hum.printAll();
int aibh = ai.bestHand();
int humbh = hum.bestHand();
if ( aibh == humbh)
System.out.println("TIE");
else
if ( aibh < humbh)
System.out.println(hum.getName() + "Wins");
else
System.out.println(ai.getName() + "Wins");
ai.reset();
hum.reset();
}
}
}
/*
* parent class for human and AI pokerPlayers
* version 1.1, updated 2009/2/5
* discard must be overridden in child classes
* 2/5 see discard function
* 2/6 hand variable made protected
*/
abstract class pokerPlayer {
private int stake;
private String name;
protected pokerHand hand;
public pokerPlayer(int s, String N){
stake = s; // for future work
name = N;
//reset();
}
/**
* Method reset
* create a new empty hand
*
*/
public void reset() {
// TODO: Add your code here
hand = new pokerHand();
}
/**
* Method takeCard
* Parameter: a Card
*
* Adds the passed Card to the player's hand
*/
public void takeCard(Card aCard) {
[COLOR="Red"]Getting error here[/COLOR] - hand.addCard(aCard);
}
/**
* Method getStake
*
* @return
*
*/
public int getStake() {
// TODO: Add your code here
return stake;
}
/**
* Method setStake
*
*
*/
public void setStake(int val) {
// TODO: Add your code here
stake = val;
}
/**
* Method printHand
*
*
*/
public void printAll() {
// TODO: Add your code here
int cc = hand.getCardCount();
for (int i = 0; i < cc; i++) {
System.out.println( hand.getCard(i).toString());
}
}
public void printVisible() {
// TODO: Add your code here
int cc = hand.getCardCount();
for (int i = 0; i < cc; i++) {
if ( i <=1)
System.out.println( hand.getCard(i).toString());
else
System.out.println( "***** Hidden *******");
}
}
/**
* Method discard
*
*
* @return
* 2/5 added parameter to allow discard to 'see' opponent's down cards
*/
//public abstract int discard( Hand downCards);
/**
* Method downCard
* Returns requested Card
* NOTE: should test parameter for range validity.
*
* @return
*
*/
public Card downCard(int i) {
// TODO: Add your code here
return hand.getCard(i);
}
public String getName() {
return name;
}
public int bestHand() {
return hand.bestHand();
}
}
/**
* An object of type Hand represents a hand of cards. The
* cards belong to the class Card. A hand is empty when it
* is created, and any number of cards can be added to it.
*/
import java.util.Vector;
public class Hand {
private Vector hand; // The cards in the hand.
/**
* Create a hand that is initially empty.
*/
public Hand() {
hand = new Vector();
}
/**
* Remove all cards from the hand, leaving it empty.
*/
public void clear() {
hand.setSize(0);
}
/**
* Add a card to the hand. It is added at the end of the current hand.
* @param c the non-null card to be added.
* @throws NullPointerException if the parameter c is null.
*/
public void addCard(Card c) {
if (c == null)
throw new NullPointerException("Can't add a null card to a hand.");
hand.addElement(c);
}
/**
* Remove a card from the hand, if present.
* @param c the card to be removed. If c is null or if the card is not in
* the hand, then nothing is done.
*/
public void removeCard(Card c) {
hand.remove(c);
}
/**
* Remove the coard in a specified position from the hand.
* @param position the position of the card that is to be removed, where
* positions are starting from zero.
* @throws IllegalArgumentException if the position does not exist in
* the hand, that is if the position is less than 0 or greater than
* or equal to the number of cards in the hand.
*/
public void removeCard(int position) {
if (position < 0 || position >= hand.size())
throw new IllegalArgumentException("Position does not exist in hand: "
+ position);
hand.removeElementAt(position);
}
/**
* Returns the number of cards in the hand.
*/
public int getCardCount() {
return hand.size();
}
/**
* Gets the card in a specified position in the hand. (Note that this card
* is not removed from the hand!)
* @param position the position of the card that is to be returned
* @throws IllegalArgumentException if position does not exist in the hand
*/
public Card getCard(int position) {
if (position < 0 || position >= hand.size())
throw new IllegalArgumentException("Position does not exist in hand: "
+ position);
return (Card)hand.elementAt(position);
}
/**
* Sorts the cards in the hand so that cards of the same suit are
* grouped together, and within a suit the cards are sorted by value.
* Note that aces are considered to have the lowest value, 1.
*/
public void sortBySuit() {
Vector newHand = new Vector();
while (hand.size() > 0) {
int pos = 0; // Position of minimal card.
Card c = (Card)hand.elementAt(0); // Minumal card.
for (int i = 1; i < hand.size(); i++) {
Card c1 = (Card)hand.elementAt(i);
if ( c1.getSuit() < c.getSuit() ||
(c1.getSuit() == c.getSuit() && c1.getValue() < c.getValue()) ) {
pos = i;
c = c1;
}
}
hand.removeElementAt(pos);
newHand.addElement(c);
}
hand = newHand;
}
/**
* Sorts the cards in the hand so that cards are sorted into order
* of increasing value. Cards with the same value are sorted by suit.
* Note that aces are considered to have the lowest value, 1.
*/
public void sortByValue() {
Vector newHand = new Vector();
while (hand.size() > 0) {
int pos = 0; // Position of minimal card.
Card c = (Card)hand.elementAt(0); // Minumal card.
for (int i = 1; i < hand.size(); i++) {
Card c1 = (Card)hand.elementAt(i);
if ( c1.getValue() < c.getValue() ||
(c1.getValue() == c.getValue() && c1.getSuit() < c.getSuit()) ) {
pos = i;
c = c1;
}
}
hand.removeElementAt(pos);
newHand.addElement(c);
}
hand = newHand;
}
}
