Results 1 to 5 of 5
  1. #1
    mj23brm is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default null pointer exeption

    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.

    Java Code:
    /**
     * @(#)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;
       }
       
    }
    Thanks for the help

  2. #2
    mj23brm is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default Never Mind im an idiot i figured it out

    sorry about the troulbe

  3. #3
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    If your error is where you say it is, it is because the variable hand is null, or hasn't been initialized yet. Examples of Null Pointers:
    Java Code:
    public class NullPointerExample {
         public NullPointerExample() { };
         private static int n;
         private static void setN() {
              n = 5;
         }
         public static void main(String[] args) {
              NullPointerExample npe; // forgot new NullPointerExample();
              setN();
         }
    }
    As you can see, The instance of NullPointerExample was never intialized, a.k.a. its value is null. So somehow your hand instance in your code didn't get initialized. Even if you didn't forget the new ClassName(arguments);, You might declare the variable or instance or field (in your case hand is an instance), but then have a method which initializes it, but never gets called. Hope this helped.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    just curious: why did you comment out the call to reset(); in your pokerPlayer constructor?

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Dang comments ...

    Never Mind im an idiot i figured it out
    I think that commented reset() is the source of the above comment :)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 03:30 AM
  3. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •