整个类中的NullPointerException

时间:2014-04-30 17:26:54

标签: java nullpointerexception null

这段代码并不接近完成,为了通过编写它来测试它我必须通过这些错误。所有类都编译但是当我运行它时会出现以下错误:

Exception in thread "main" java.lang.NullPointerException
at Deck.<init>(Deck.java:41).
at Dealer.<init>(Dealer.java:25).
at Poker.main(Poker.java:16).

以下是我目前的代码。发生错误时出现的粗体

public class Dealer
{
    private Deck deck;
    private Player[] players;

    public static final int NUMBER_OF_PLAYERS = 2;

    /**
     * Default constructor - creates and populates the Player array and the
     * Deck object.
     */
   public Dealer()
   {
      players = new Player[ NUMBER_OF_PLAYERS ];        
      **deck = new Deck();**

    // populate the array of players

    } // constructor

    /**
     * Outermost level of abstraction for the poker game
     */
    public void play()
    {
      System.out.println("Welcome to Poker!");

    } // method play
}

public class Poker
{
    public static void main( String[] args )
    {
        Dealer dealer;
        **dealer = new Dealer();**

        dealer.play();
    }

}

public class Card
{
    private int face;
    private int suit;

    public static final int CLUBS = 100;
    public static final int DIAMONDS = 200;
    public static final int HEARTS = 300;
    public static final int SPADES = 400;

    /**
     * Default Value Constructor - for stubs only!!!
     */
    public Card()
    {
        // do nothing in it

    } // default value constructur


    /**
     * Explicit Value Constructor
     */
    public Card( int cardFace, int cardSuit )
    {
        face = cardFace;
        suit = cardSuit;

    } // constructor


    /**
     * Returns the face of the card as a String
     *
     * @return the face of the card
     */
    public String getFace()
    {
        String returnVal;

        switch ( face )
        {
            case 11: returnVal = "Jack"; break;
            case 12: returnVal = "Queen"; break;
            case 13: returnVal = "King"; break;
            case 14: returnVal = "Ace"; break;
            default: returnVal = "" + face;

        } // end switch

        return returnVal;

    } // method getFace


    /**
     * Return the numeric face value
     *
     * @return the numeric face value
     */
    public int getFaceValue()
    {
        return face;

    } // method getFaceValue


    /**
     * Returns the suit of the card as a String
     *
     * @return the suit of the card as a String
     */
    public String getSuit()
    {
        String returnVal;
        returnVal = "Error";

        switch ( suit )
        {
            case CLUBS: returnVal = "Clubs"; break;
            case DIAMONDS:returnVal = "Diamonds"; break;
            case HEARTS: returnVal = "Hearts"; break;
            case SPADES: returnVal = "Spades";

        } // end switch

        return returnVal;

    } // method getSuit


    /**
     * Return the numeric suite value
     *
     * @return the numeric suite value
     */
    public int getSuitValue()
    {
        return suit;

    } // method getSuitValue()


     /**
     * Returns true of the two cards have the same value and the same suit.
     * Use this to test for validity (this should never happen!!!).
     *
     * @param the card to compare with this one
     * @return true if the 2 cards have the same face value and suit
     */
    public boolean equals( Card other )
    {
        return getFaceValue() == other.getFaceValue() &&
               getSuitValue() == other.getSuitValue();

    } // method equals


    /**
     * Return the card as a String
     *
     * @return the String value for the card
     */
    public String toString()
    {
        return getFace() + " of " + getSuit();

    } // method toString

}

public class Deck
{
    private int pointer = 0; // indicates the current position in the deck.
                         // This should begin with 0 (the first call)
                         // and increment every time a card is dealt.

    private Card deck[];
    private Card tempDeck[];
    private Card Card[];

    public static final int CARDS_IN_DECK = 52;

    /**
     * Instantiate an array of Cards and populate the array with 52 Card 
     * objects.  The face values of the cards should be between 2 - 14.  
     * Values 2 - 10 represent the number cards.  Values 11 - 14 represent 
     * Jack, Queen, King, and Ace, respectively.  The suits should be as 
     * follows: 100 = Clubs, 200 = Diamonds, 300 = Hearts, and 400 = Spades.  
     * See the Card class for more information.
     *
     * You should both shuffle and cut the cards before this method 
     * concludes.
     */
    public Deck()
    {
      int i = 0;
      for(int a = 1; a <= 4; a++)
      {
         for(int b = 2; b <=14; b++)
         {
            **deck[i] = new Card(a,b);**
         if(deck[i] == null)
         {
            System.out.println("Shouldnt get here");
         }
         i++;
         }
      }

      shuffle();
      cut();

    } // default constructor


    /**
     * Cut the deck.  Choose a point in the deck (this can be either random 
     * or fixed) and re-arrange the cards.  For example, if you choose to 
     * cut at position 26, then the 27th - 52nd cards will be placed in the 
     * 1st - 26th positions.  The 1st - 26th cards will be placed in the 
     * 27th - 52nd positions.
     */
    public void cut()
      {
         int cut = 26;
         int a = 0;
         int b = 0;

         for(int i = 0 ; i<cut; i++)
         {
            tempDeck[i] = new Card(a,b);
            tempDeck[i] = deck[i+26];
            tempDeck[i+26] = deck[i];
         }

         deck = tempDeck; 
      }

 // method cut

    /**
     * Deal 5 cards from the deck. Deal out the next 5 cards and return 
     * these in an array.  You will need to maintain a pointer that lets
     * you know where you are in the deck.  You should make sure also
     * to reshuffle and cut the deck and start over if there are not enough 
     * cards left to deal a hand.
     *
     * @return an array of 5 cards
     */
   public Card[] deal(int[] args)
   {
      int i = 0;
      int a = 0;
      int b = 0;
      Card[i] = new Card(a,b);

      for(i = 0; i < 5; i++)
      {
         Card[pointer] = deck[pointer];
         pointer++;
      }               

      return Card; 
         // this is a stub only - replace this!!!!

    } // method deal


    /**
     * Get a card from the deck
     *
     * @param the position of the card you are retrieving
     * @return the card object
     */
   public Card getCard( int card )
   {
      Card oneCard = deck[pointer];
      deck[pointer] = null;
      pointer +=1;

      return oneCard; // this is a stub only - replace this!!!

   } // method getCard


    /**
     * Shuffle the deck.  Randomly re-arrange the cards in the deck. There
     * are plenty of algorithms for doing this - check out Google!!!
     */
      public void shuffle()
      {
         int i, j, k;
         int n = 15;

         for ( k = 0; k < n; k++ )
         {
          i = (int) ( CARDS_IN_DECK * Math.random() );  // Pick 2 random cards
          j = (int) ( CARDS_IN_DECK * Math.random() );  // in the deck

          Card tmp = deck[i];
          deck[i] = deck[j];
          deck[j] = tmp;
       }

       pointer = 0;   // Reset current card to deal

      } // end shuffle

}

public class Player
{
    private int bet;
    private int chips;
    int totalChips;

    private Hand hand;
    private String name;


    public static final int START_CHIPS = 100;
    public static final int WINNING_CHIPS = 200;

    Scanner scan = new Scanner(System.in);

    /**
     * Sets the player's name and the starting number of chips.
     *
     * @param the player's name
     */
   public Player( String n )
   {
         System.out.println("Enter name: ");
         name = scan.nextLine();

         totalChips = START_CHIPS;

   } // constructor


    /**
     * Sets the amount of the bet and decreases the number of chips that 
     * the player has by the number of chips bet. Do not allow bets if 
     * there are not enough chips left.
     *
     * @param the number of chips bet
     * @return true if the bet was successful (there were enough chips)
     */
   public boolean bet( int bet )
   {
      int chipsAB;
      boolean canBet;

      //Get Bet
      getBet();

      //Se if player has enough chips for bet
      if(chips >= bet)
      {
         chipsAB = chips - bet;
         canBet = true;
      }
      else
      {
         System.out.println("You do not have enough chips.");
         canBet = false;        
      }

      return canBet; // this is a stub only - replace this!!!!

   } // method bet


    /**
     * Return the number of chips bet
     *
     * @return the number of chips bet
     */              //DONE
    public int getBet()
    {
         int bet;

         System.out.println("Enter bet: ");
         bet = scan.nextInt();

         while (bet < 1 || bet > getChips())
         {
            System.out.println("Error. Re-enter bet: ");
            bet = scan.nextInt();
         }

         return bet; // this is a stub only - replace this!!!!

    } // method getBet

    /**
     * Return the number of chips currently held
     *
     * @return the number of chips held
     */
   public int getChips()
   {
      int totalChips = 0;

      totalChips = winHand(); 

      return totalChips; // this is a stub only - replace this!!!!

   } // method getChips


    /**
     * Return the player's hand
     *
     * @return the player's hand object
     */
    public Hand getHand()
    {
        return new Hand(); // this is a stub only - replace this!!!!

    } // method getHand


    /**
     * Return the player's name
     *
     * @return the player's name
     */
    public String getName()
    {
        return name; // this is a stub only - replace this!!!!

    } // method getName

    /**
     * Indicates whether this player has won
     *
     * @return true if the player has more than the number of winning points
     */
    public boolean hasWon()
    {
         boolean won = false;

         if(chips == 0)
         {
            won = true;
         }

        return won; // this is a stub - replace this!!!

    } // method hasWon


    /**
     * Set the Hand object to the incoming Hand object (this comes from the 
     * Dealer)
     *
     * @param the hand dealt by the dealer
     */
    public void setHand( Hand h )
    {


    } // method setHand


    /**
     * Return the player's name & the number of chips
     *
     * @return the players name & number of chips
     */
    public String toString()
    {
        String nameChips;
        nameChips = (name + totalChips);
        return nameChips; // this is a stub only - replace this!!!

    } // method toString


    /**
     * We won the hand, so increase chips
     *
     * @param the number of chips won
     */
    public int winHand()
    {
      int chipsAB = 0;
      //if(hand.beats(other))
      {
         chipsAB = getChips() + getBet();
      }
      //else
         chipsAB = getChips() - getBet();

      return chipsAB;

    } // method winHand


}

1 个答案:

答案 0 :(得分:1)

deck课程中的Deck成员未初始化 - 即null。 当您尝试通过引用deck[i]初始化其元素时,它将失败。要更正此问题,您只需初始化它:

private Card deck[] = new Card[CARDS_IN_DECK];