与我的二十一点卡程序中的NullPointerexception错误相混淆

时间:2011-05-03 20:25:00

标签: java programming-languages nullpointerexception

我正在制作黑色插卡游戏。我对我的程序错误感到困惑。任何帮助将受到高度赞赏。提前谢谢!
这是我运行代码时出现的错误:

线程“main”java.lang.NullPointerException中的异常     在BlackJackCardGame.DealHands(BlackJackCardGame.java:197)     在BlackJackCardGame.PlayBlackJack(BlackJackCardGame.java:207)     在BlackJackCardGame.main(BlackJackCardGame.java:252)

我的代码如下:

 import java.util.Scanner;
//import java.util.*;
public class BlackJackCardGame 
{
static class Player
{
    private String Name;
    private int handValue;
    private boolean BlackJack;
    private TheCard[] Hand;
    public Player(String name)
    {
        this.Name = name;
        this.handValue = 0;
        this.BlackJack = false;
        this.Hand = null;
    } 
}
private static Player[] InitializePlayers(int PlayerCount)
{
    Player[] thePlayers = new Player[PlayerCount + 1];
    for(int i = 0; i < PlayerCount + 1; i++)
    {
        String tempName;
        if (i == 0)
        {
            tempName = "Dealer"; 
        }
        else
        {
            tempName = "Player_" + String.valueOf(i);
        }
        thePlayers[i] = new Player(tempName);
    }
    return thePlayers;
}

static class TheCard
{
    // Java getter & setter
    private String CardName;
    private int CardRank;
    private int Chosen;

    public TheCard(int rank, String name)
    {
        this.CardName = name;
        this.CardRank = rank;
        this.Chosen = 0;
    }
}

private static TheCard[] BuildDeck(int decks)   
{
    String[] Cards = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
    String[] Suits = {"Spades","Hearts","Diamonds","Clubs"};
    TheCard[] theDeck = new TheCard[Cards.length * Suits.length];
    //int String [] = {2,3,4,5,6,7,8,9,10,11,12,13,14};
    int[] Rank = {2,3,4,5,6,7,8,9,10,10,10,10,11};
    int cardnumber = 0;
    for (int d = 0; d < decks; d++)
    {
        for (int i = 0; i < Cards.length; i++)
        {
            for (int j = 0; j < Suits.length; j++)
            {
                String deckcard = Cards[i];
                String suitcard = Suits[j];
                String cardname = deckcard + "-" + suitcard;
                theDeck[cardnumber] = new TheCard(Rank[i], cardname);  
                cardnumber++;
            }
        }
    }   
    return theDeck;
}

private static TheCard Deal(TheCard[] OrderedDeck) 
{   // this method uses Random method to "deal" a single card from the playing deck
    TheCard thecard;
    int NumberofCards = OrderedDeck.length;
    int random = (int) (NumberofCards*Math.random ());
    thecard = OrderedDeck[random];
    if (thecard.Chosen == 0 )       // if available... 
    {
        thecard.Chosen = 1;         // mark it taken...
        return thecard;
    }
    else
    {
        return Deal(OrderedDeck);
    }
}
private static int ShowCardOfDealer(Player player, int cardNumber)
{
    System.out.println (player.Name + ", is holding a: ");
    System.out.println (player.Hand[cardNumber].CardName);
    int value = player.Hand[cardNumber].CardRank;
    System.out.println ("..with value of: " + String.valueOf(value));
    return value;
}
private static void DealerPlays(TheCard[] deck,Player[] players)
{
    Player currentPlayer = players[0];     // dealer first in array
    int handValue = ShowHand(currentPlayer);
    int choice = 1;
        do
        {
            if (handValue < 17 )
            {
                TheCard newCard = Deal(deck);
                int numCards = currentPlayer.Hand.length;
                currentPlayer.Hand[numCards + 1] = newCard;
                handValue = ShowHand(currentPlayer);
                if (handValue > 21) 
                {
                    System.out.println ("The Dealer has busted!");
                    handValue = 0;  // special signal that this value always loses
                    choice = 0;     
                }
            }
            else
            {
                System.out.println ("The Dealer stays.");
                choice = 0;  //dealer is forced to stay, =>17
            }

        } while ( choice == 1);
}
private static void MakeChoices(TheCard[] deck,Player[] players)
{
    for( int i = 1; i < players.length -1 ; i++ )
    {
        Player currentPlayer = players[i];
        int handValue = ShowHand(currentPlayer);
        Scanner input = new Scanner(System.in);
        int choice = 0;
        do
        {
            System.out.println ("Make your choice please. Type 1 for Hit or type 0 for Stay.");
            choice = input.nextInt();
            if (choice == 1) 
            {
                // DealAnotherCardToPlayerX
                // what player is going to be updated
                // add new card to players hand
                TheCard newCard = Deal(deck);
                int numCards = currentPlayer.Hand.length;
                currentPlayer.Hand[numCards + 1] = newCard;
                handValue = ShowHand(currentPlayer);
                if (handValue > 21) 
                {
                    System.out.println ("You have busted!");
                    handValue = 0;  // special signal that this value always loses
                    choice = 0;     //this guy is done, loop to next player
                }
            }
        } while ( choice == 1);
        currentPlayer.handValue = handValue;
    }
}
private static void setBlackJackCase(Player player)
{
    player.BlackJack = false;
    if (player.Hand[0].CardRank == 10 && player.Hand[1].CardRank == 11) 
    {
        player.BlackJack = true;
    }
    if (player.Hand[1].CardRank == 10 && player.Hand[0].CardRank == 11) 
    {
        player.BlackJack = true;
    }
}
private static int ShowHand(Player player)
{
    int cards = player.Hand.length;     // get number of cards player x has
    System.out.println (player.Name + ", you are holding: ");
    int value = 0;
    for (int c = 0; c < cards; c++ )
    {
        System.out.println (player.Hand[c].CardName);
        //value = value + player.Hand[c].CardRank;
        value += player.Hand[c].CardRank;
    }
    setBlackJackCase(player);
    System.out.println ("Your total card value is: " + String.valueOf(value));
    return value;
}
private static void DealHands(TheCard[] deck,Player[] players)
{
    for (int c = 0; c < 2; c++)
    {
        for( int i = 1; i < players.length -1 ; i++ )
        {
            TheCard card = Deal(deck);
            players[i].Hand[c] = new TheCard(card.CardRank, card.CardName);
        }
        //give dealer card
        TheCard card = Deal(deck);
        players[0].Hand[c] = new TheCard(card.CardRank, card.CardName);
    }
}
private static void PlayBlackJack(TheCard[] playingDeck, Player[] players)
{

    DealHands( playingDeck, players);    // everybody has their hands - ready for choices:
    ShowCardOfDealer ( players[0], 0);  //shows dealer's turned up card
    MakeChoices (playingDeck, players);  // everybody is either out or at "stay"
    DealerPlays (playingDeck, players);  // Dealer "plays" and Dealer Rules
    AnnounceWinners (players);
}
private static void AnnounceWinners(Player[] players)
{
    int dealerHand = players[0].handValue;
    for( int i = 1; i < players.length -1 ; i++ )
    {
        int playerHand = players[i].handValue;
        if (players[i].BlackJack) 
        {
                System.out.println (players[i].Name + ", you have BlackJack! :) You WIN!");
        }
        else
        {
            if (dealerHand == 0 && playerHand == 0)
            {
                System.out.println (players[i].Name + " has busted..");
            }
            if (dealerHand >= playerHand )
            {
                System.out.println ("Dealer wins" );
            }
            else
            {
                System.out.println (players[i].Name + ", you WIN!");
            }
        }
    }   
}
public static void main(String args[])
{

    System.out.println ("Welcome, to the game of Black Jack");
    Scanner input = new Scanner(System.in);
    System.out.println ("How many decks of cards are in this game? Enter a number from 1 to 3.");
    int decks = input.nextInt();
    System.out.println ("How many players are in this game? (Not counting the Dealer)");
    int numPlayers = input.nextInt();
    TheCard[] PlayingDeck = BuildDeck(decks);
    Player[] thePlayers = InitializePlayers(numPlayers);
    //loop 
        PlayBlackJack(PlayingDeck, thePlayers);
        System.out.println ("Play Again? Type 'y' or 'n'");
    //test answer
}
}

5 个答案:

答案 0 :(得分:2)

您没有提供足够的信息来轻松指出问题,但您可以获得所需的所有信息,因为您可以看到行号。找到第197行并查看该行上的每个对象。其中一个是null,你试图把它看作是一个有效的对象。

private static void DealHands(TheCard[] deck,Player[] players)
{
    for (int c = 0; c < 2; c++)
    {
        for( int i = 1; i < players.length -1 ; i++ )
        {
            TheCard card = Deal(deck);
            players[i].Hand[c] = new TheCard(card.CardRank, card.CardName);
        }
        //give dealer card
        TheCard card = Deal(deck);
        players[0].Hand[c] = new TheCard(card.CardRank, card.CardName);
    }
}

我的猜测是你没有在Hand构造函数中初始化播放器的Player,因此players[i].Hand[c]会尝试访问null数组中的索引。首先需要将数组初始化为某个长度。

答案 1 :(得分:1)

请注意Player构造函数中的以下代码行:this.Hand = null;

答案 2 :(得分:1)

您没有正确初始化Player.Hand数组

你设置的Player类构造函数(第16行)中的

this.Hand = null;

在您的方法中

private static Player[] InitializePlayers(int PlayerCount)

在第19行,当你尝试执行以下操作时,不要更新它,所以在第196行(nullpointer):

players[i].Hand[c] = new TheCard(card.CardRank, card.CardName);

玩家[i] .Hand []为空,所以你得到错误。确保在InitializePlayer方法中将Hand设置为非null数组。

(顺便说一句,普通的Java约定是方法和类成员名称的较低的驼峰情况,例如“Hand”等变量应该是“hand”,方法“InitializePlayer”应该像“initializePlayer”)

答案 3 :(得分:1)

public Player(String name) {
        this.Name = name;
        this.handValue = 0;
        this.BlackJack = false;
        this.Hand = null;
}

检查行this.Hand = null; 然后你用Hand at:

private static void DealHands(TheCard[] deck, Player[] players) {
    for (int c = 0; c < 2; c++) {
        for (int i = 1; i < players.length - 1; i++) {
            TheCard card = Deal(deck);
            players[i].Hand[c] = new TheCard(card.CardRank, card.CardName);
        }
        // give dealer card
        TheCard card = Deal(deck);
        System.out.print("Card == null" + card == null);

        players[0].Hand[c] = new TheCard(card.CardRank, card.CardName);
    }

}

这里在线:     玩家[0]。手[c] =新的TheCard(card.CardRank,card.CardName);

所以试着宣布这个.Hand = new TheCard [2]; //这里2只是一个例子

然后,您的代码中还有另一个问题

private static void DealerPlays(TheCard[] deck, Player[] players) {
    Player currentPlayer = players[0]; // dealer first in array
    int handValue = ShowHand(currentPlayer);
    int choice = 1;
    do {
        if (handValue < 17) {
            TheCard newCard = Deal(deck);
            int numCards = currentPlayer.Hand.length;
            currentPlayer.Hand[numCards + 1] = newCard;
            handValue = ShowHand(currentPlayer);
            if (handValue > 21) {
                System.out.println("The Dealer has busted!");
                handValue = 0; // special signal that this value always
                                // loses
                choice = 0;
            }
        } else {
            System.out.println("The Dealer stays.");
            choice = 0; // dealer is forced to stay, =>17
        }

    } while (choice == 1);
}

检查下面的行并修复它,这是类型“ArrayIndexOutOfBoundsException”的肯定例外。

int numCards = currentPlayer.Hand.length;
currentPlayer.Hand[numCards + 1] = newCard;

答案 4 :(得分:0)

堆栈顶部跟踪引用第197行。除非您的源代码段缺少行,否则第197行是:

players[i].Hand[c] = new TheCard(card.CardRank, card.CardName);

每次使用点运算符.访问对象时,如果被访问的对象为null,则会抛出NPE。尝试索引引用为null的数组变量时,也会抛出NPE。所以嫌疑人是:

players
players[i]
players[i].Hand
card

以上其中一项必须是null。我建议使用调试器在第197行中断并检查每个嫌疑人。一旦你发现哪一个是罪魁祸首,你就可以向后工作并试图弄清楚它是如何成为null的。{/ p>

(请注意,上面未包含card.CardRankcard.CardName;这是因为如果它们是问题,那么您的异常将从TheCard的构造函数中抛出。)