我正在做一个黑杰克纸牌游戏。我对程序出现的错误感到困惑。任何帮助将不胜感激。提前致谢!
这是我运行代码时遇到的错误:

线程“主”中的异常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
}
}

最佳答案

您没有提供足够的信息来轻松指出问题,但由于可以看到行号,因此您拥有了所需的所有信息。找到第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数组中的索引。首先,您需要将数组初始化为一定长度。

10-07 12:02