перепутал с ошибкой NullPointerexception в моей программе карты блэкджека - PullRequest
0 голосов
/ 04 мая 2011

Я делаю карточную игру Блэк Джек. Я запутался из-за ошибки в моей программе. Любая помощь будет высоко оценен. Заранее спасибо!
Это ошибка, которую я получаю, когда запускаю свой код:

Исключение в потоке "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 ]

2 голосов
/ 04 мая 2011

Вы не предоставили достаточно информации, чтобы легко указать на проблему, но у вас есть вся необходимая информация, так как вы можете видеть номера строк. Найдите строку 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 голос
/ 04 мая 2011
public Player(String name) {
        this.Name = name;
        this.handValue = 0;
        this.BlackJack = false;
        this.Hand = null;
}

проверьте строку this.Hand = null;а затем вы использовали Hand на:

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);
    }

}

Here In the line: игроки [0] .Hand [c] = новая карта (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;
1 голос
/ 04 мая 2011

Вы неправильно инициализируете массив Player.Hand

в конструкторе класса Player (строка 16), который вы установили

this.Hand = null;

А по твоему методу

private static Player[] InitializePlayers(int PlayerCount)

в строке 19 вы не обновляете это, поэтому в строке 196 (нулевой указатель) при попытке сделать следующее:

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

Players [I] .Hand [] имеет значение null, поэтому вы получите свою ошибку. Убедитесь, что в методе InitializePlayer для Hand задан ненулевой массив.

(Кроме того, обычное Java-соглашение - это нижний регистр верблюдов для имен методов и членов класса, например, переменные, такие как "Hand", должны быть "hand", а методы "InitializePlayer" должны быть похожи на "initializePlayer")

1 голос
/ 04 мая 2011

Обратите внимание на эту строку кода в конструкторе Player: this.Hand = null; .

0 голосов
/ 04 мая 2011

Верх вашей строки ссылок трассировки стека 197. Если в вашем исходном фрагменте отсутствуют строки, строка 197 выглядит следующим образом:

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

Каждый раз, когда вы получаете доступ к объекту с помощью оператора точки ., NPE будет выброшен, если объект, к которому осуществляется доступ, равен null. NPE также может быть сгенерирован при попытке индексации в переменную массива, ссылка на которую null. Итак, подозреваемые:

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

Один из вышеперечисленных должен быть null. Я предлагаю использовать отладчик, чтобы разбить строку 197 и осмотреть каждого подозреваемого. Как только вы найдете, кто из них является виновником, вы можете вернуться назад и попытаться выяснить, как он стал null.

(Обратите внимание, что card.CardRank и card.CardName не включены выше; это потому, что если бы они были проблемой, ваше исключение было бы выброшено из конструктора для TheCard.)

...