线程“main”java.lang.NullPointerException中的异常?

时间:2011-03-04 17:18:08

标签: java

我是java编程的初学者。我正在尝试重新创建卡片游戏大战的简化版本。我运行了下面发布的程序,然后又出现了这个错误:

线程“main”中的异常java.lang.NullPointerException

在cardgame.BuildDeck(cardgame.java:36)

cardgame.main上的

(cardgame.java:60)

我一直试图通过研究自己解决这个问题,但我无法解决。我想知道是否有人可以帮助我。如果您确实需要有关我的计划的任何其他信息,请询问。提前谢谢!

-FGxMatta

public class cardgame 
{
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;
    }
}


@SuppressWarnings("null")
private static TheCard[] BuildDeck()
{
    TheCard[] TheDeck = null;
    String[] Cards =  {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
    String[] Suits = {"Spades","Hearts","Diamonds","Clubs"};
int[] Rank = {2,3,4,5,6,7,8,9,10,11,12,13,14};
    int cardnumber = 0;
    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 GetRandomCard(TheCard[] OrderedDeck) 
{
    TheCard thecard;
    int random = (int) (51*Math.random ());
    thecard = OrderedDeck[random];
    if (thecard.Chosen == 0 )       // if available... 
    {
        thecard.Chosen = 1;         // mark it taken...
        return thecard;
    }
    else
    {
        return GetRandomCard(OrderedDeck);
    }
}

public static void main(String args[])
{
    TheCard[] OrderedDeck = BuildDeck();
    System.out.println ("Welcome, Prepare for War!");
    int decksize = OrderedDeck.length;
    int player1wincount = 0;
    int player2wincount = 0;
    int tiecount = 0;
    for (int cardcount = 0; cardcount < decksize;)
    {
        TheCard Player1 = GetRandomCard(OrderedDeck);
        cardcount++;
        TheCard Player2 = GetRandomCard(OrderedDeck);
        cardcount++;
        System.out.println ("Player 1's card is: " + Player1.CardName);
        System.out.println ("Player 2's card is: " + Player2.CardName);
        if (Player1.CardRank > Player2.CardRank)
        {
            System.out.println("Player 1 wins this hand");
            player1wincount++;
        }
        if (Player1.CardRank < Player2.CardRank)
        {
            System.out.println("Player 2 wins this hand");
            player2wincount++;
        }
        if (Player1.CardRank == Player2.CardRank)
        {
            System.out.println("Player 1 and Player 2 played the same valued card");
            tiecount++;
        }
    }
    System.out.println ("Player 1 won " + String.valueOf(player1wincount) + " hands");
    System.out.println ("Player 1 won " + String.valueOf(player2wincount) + " hands");
    System.out.println ("There were " + String.valueOf(tiecount) + " ties");
}
}

5 个答案:

答案 0 :(得分:10)

替换:

TheCard[] theDeck = null;

使用:

TheCard[] theDeck = new TheCard[Cards.length * Suits.length];

并将其移至CardsSuits的声明下方。

答案 1 :(得分:3)

空指针异常是代码中的一种情况,您尝试访问/修改尚未初始化的对象。它本质上意味着对象引用变量不指向任何地方并且不引用任何内容或“null”。一个简单的例子可以是:

package au.com.copl;
public class Demo{
    public static void main(String[] args) {
        String d = null;
        System.out.println(d.toString()); // d is un-initialized and is null
    }
}

答案 2 :(得分:1)

就在这里

TheDeck[cardnumber] = new TheCard(Rank[i], cardname);  

你从未初始化TheDeck。你可能想要像

这样的东西
TheDeck = new TheCard[52];

在开始将数据放入数组之前。

作为一个注释,java约定是变量名称是camel cased。所以“CardName”应该是cardName。只是一个惯例。

答案 3 :(得分:1)

TheCard[] theDeck = null;

你不需要初始化吗?

答案 4 :(得分:0)

您还应该在main方法中使用new

TheCard[] OrderedDeck = BuildDeck();

应替换为:

TheCard[] OrderedDeck = new BuildDeck();