编写一个 Blackjack 程序,当我试图打印出洗牌后的牌时得到 nullpointerEx

Writing a Blackjack program, getting nullpointerEx when I tried to printout the shuffled deck

基本上我尝试在测试程序中使用 运行 deckTest() 来打印出 post-洗牌的牌组(删除 deckTest 上的第一条和最后一条评论)。我得到的只是 Deck class.

中 toString 方法中行 answer=answer+ "\n" + deck[i].toString(); 上的 nullpointerEx

但是当我尝试打印出预洗牌(带有 testDeck() 中的评论)时,没问题。

从 deckTest() 中的第二个注释实际上显示洗牌后的第一张牌这一事实来看,我认为洗牌至少有效!

感谢你们所有人,祝 2015 年愉快! 威廉 S

有测试程序:

public class TestCard
{



    public static void main ()
    {
        Card c1=new Card(0,1);
        Card c2=new Card(3,13);
        System.out.println(c1.toString());
        System.out.println(c2.toString());

    }
    public static void deckTest()
    {
        Deck d1= new Deck();
        System.out.println(d1);

        //d1.shuffle();
        //System.out.println(d1.dealCard().toString());
        //System.out.println(d1.toString());

    }
}

它所指的牌组 class:

import java.util.Random;// for shuffing the cards


public class Deck
{


    private final int NUMOFCARDS= 52;
    private int numCards;

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

    /**
     * Constructor for objects of class deck
     */
    public Deck()
    {
        int c = 0;
        for (int s=0;s<=3;s++)
        {
            for (int n=1;n<=13;n++){

                deck[c]=new Card(s,n);
                c++;    
            }
        }
        numCards=NUMOFCARDS;
    }

    /**
     * Use this method to display all of the cards in the deck
     */
    public String toString()
    {
        String answer = "";

        for (int i=0;i<53;i++)
        {

            answer= answer + "\n" + deck[i].toString();
        }



        return answer;

    }

    /**
     * returns true of the current number of cards in the deck equals to 0
     */
    public boolean empty()
    {
        return numCards==0;
    }

    /**
     * pull the bottem card from the deck
     * the variable 
     */
    public Card dealCard()
    {
        if (empty())
        {
            System.out.println("the deck has run out of cards, there will be a new,preshufffled deck to continue");
            //shuffle();  //shuffle cards
            numCards=NUMOFCARDS; //reset dealPosition for dealing new deck            
        }

        numCards--;
        return deck[52-numCards];        
    }

    public void shuffle()
    {
        Random random = new Random(); // creat a random object

        Card memory;
        int randomPosition ;
        for (int i=0;i<53;i++)
        {
            randomPosition = random.nextInt(53); // assign a number between 0 to 52 as randomPosition for shuffle
            memory=deck[i];   // store the current deck[i] card 
            deck[i]=deck[randomPosition];    //assign new card to current card 
            deck[randomPosition]=memory;    //assign current card to new card 
        }

    }
}

有卡牌class,牌组和测试方法参考:

public class Card
{
    final int JACK = 11;
    final int QUEEN = 12;
    final int KING = 13;
    final int ACE = 1;



    private int num;
    private int suit;
    final int SPADES = 0;
    final int HEARTS = 1;
    final int DIAMONDS = 2;
    final int CLUBS = 3;




    public Card(int theSuit,int theNum)
    {
        num=theNum;
        suit=theSuit;
    }

    public String showSuit()
    {
        if (suit==0)
        {
            return "Spades";
        }
        if (suit==1)
        {
            return "Hearts";
        }
        if (suit==2)
        {
            return "Diamonds";
                    }
        if (suit==3)
        {
            return "Clubs";
        }
        return "";
    }

    public String showNum()
    {
        if (num==11)
        {
            return "Jack";
        }
        if (num==12)
        {
            return "Queen";
        }
        if (num==13)
        {
            return "King";
        }
        if (num==1)
        {
            return "Ace";
        }

        return ""+num;
    }


    public String toString()
    {
        return " "+ showNum() + " of " + showSuit();
    }



    public boolean equals(Card theCard)
    {
       return theCard.toString().equals(toString());
    }

answer=answer+ "\n" + deck[i].toString();

如果 deck[i] 尚未初始化,将抛出 NullPointerException。当使用隐式参数 null 调用 toString() 时抛出此类异常。

确保你已经在这行之前初始化了 deck[i]。

查看您的代码,您似乎没有在索引 52 处初始化卡。

变化:

/**
 * Use this method to display all of the cards in the deck
 */
public String toString()
{
    String answer = "";

    for (int i=0;i<53;i++)
    {

        answer= answer + "\n" + deck[i].toString();
    }



    return answer;

}

对此:

/**
 * Use this method to display all of the cards in the deck
 */
public String toString()
{
    String answer = "";

    for (int i=0;i<52;i++)
    {

        answer= answer + "\n" + deck[i].toString();
    }



    return answer;

}

正如之前的发帖者所说,您将得到一个空指针异常,因为您只在 deck 中初始化了 0-51,但正在使用您的逻辑打印 0-52。

问题是您正在传递牌组中的最后一张牌。 你的 Deck 构造器上有:4 * 13 = 52 张牌。因此,您的数组将在位置 0、1、...、51 处有卡片。但是在 toString 方法中,您迭代到第 52 个索引中的元素,该元素不存在!

将您的方法 toString 更改为:

@Override
public String toString() {
    String answer = "";
    for (int i = 0; i < numCards; i++) {
        answer = answer + "\n" + deck[i].toString();
    }
    return answer;
}

请看 dealCard 方法从牌组中移除了一张牌。将迭代固定为 52 可能会在移除更多卡片时导致 NullPointerException,因此最好使用 numCards。

除此之外,您的方法 shuffle 也会因同样的原因而遭受 NullPointerException。将该方法更改为:

public void shuffle() {
    final Random random = new Random(); // creat a random object

    Card memory;
    int randomPosition;
    for (int i = 0; i < numCards; i++) {
        randomPosition = random.nextInt(numCards); // assign a number
                                                    // between 0 to
                                                    // 52 as randomPosition
                                                    // for
                                                    // shuffle
        memory = deck[i]; // store the current deck[i] card
        deck[i] = deck[randomPosition]; // assign new card to current card
        deck[randomPosition] = memory; // assign current card to new card
    }

}