写一个二十一点程序,当我试图打印出洗牌的甲板时得到nullpointerEx

时间:2015-01-18 03:29:20

标签: java arrays nullpointerexception tostring blackjack

基本上我试图在测试程序中运行deckTest()来打印后洗牌的牌组(删除deckTest上的第一个和最后一个评论)。我得到的只是在Deck类的toString方法中answer=answer+ "\n" + deck[i].toString();行上的nullpointerEx。

但是当我试图打印出Pre-shuffled deck(带有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());

    }
}

它指的是甲板类:

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 
        }

    }
}

甲板和测试方法指的是卡类:

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

3 个答案:

答案 0 :(得分:0)

该行

answer=answer+ "\n" + deck[i].toString();
如果deck [i]尚未初始化,

将抛出NullPointerException。当使用隐式参数null调用toString()时,抛出此类异常。

确保您已在此行之前初始化了deck [i]。

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

答案 1 :(得分:0)

变化:

/**
 * 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;

}

正如之前的海报所述,你会得到一个空指针异常,因为你只在套牌中初始化了0-51,但是用你所拥有的逻辑打印0-52。

答案 2 :(得分:0)

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

将您的方法更改为:String:

@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
    }

}