实例化对象抛出空指针异常

时间:2018-07-13 14:48:24

标签: java arrays class object nullpointerexception

我对编程非常陌生,作为一个业余项目,我正在玩纸牌游戏,并且有两个课程:一个用于Card对象:

public class Card {

public enum Suit 
{
    CLUBS, DIAMONDS, HEARTS, SPADES
} //enum type for suit

public enum Rank
{
    ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,
    EIGHT, NINE, TEN, JACK, QUEEN, KING
} //enum type for rank

public Suit suit;
public Rank rank;
public int value;
public String name;

public Card(int suitNo, int rankNo) 
{
    this.value = rankNo;

    switch (suitNo) 
    {
        case 0:
        suit = Suit.CLUBS;
        break;

        case 1:
        suit = Suit.DIAMONDS;
        break;

        case 2:
        suit = Suit.HEARTS;
        break;

        case 3:
        suit = Suit.SPADES;
        break;

    } // switch SUIT

    switch (rankNo) 
    {
        case 0:
        rank = Rank.TWO;
        break;

        case 1:
        rank = Rank.THREE;
        break;

        case 2:
        rank = Rank.FOUR;
        break;

        case 3:
        rank = Rank.FIVE;
        break;

        case 5:
        rank = Rank.SIX;
        break;

        case 4:
        rank = Rank.SEVEN;
        break;

        case 6:
        rank = Rank.EIGHT;
        break;

        case 7:
        rank = Rank.NINE;
        break;

        case 8:
        rank = Rank.TEN;
        break;

        case 9:
        rank = Rank.JACK;
        break;

        case 10:
        rank = Rank.QUEEN;
        break;

        case 11:
        rank = Rank.KING;
        break;

        case 12:
        rank = Rank.ACE;
        break;

    } //switch RANK

    name = rank.toString() + " OF " + suit.toString();
 }
 }

另一类是套牌:

public class Deck 
{

Card[] cards; //declaring an array of cards

public void Deck () 
{
cards = new Card[52]; //declaring an array of cards with array size
int count = 0;

for (int i = 0; i < 13; i++) 
{
    for (int j = 0; j < 4; j++)
    {

        cards[count] = new Card(j, i); //instantiating the cards
        count++;

    }
}

}

但是,当我用其构造函数实例化甲板对象时,我有一个空的甲板对象。我以为Deck构造函数应该通过循环将Card对象实例化到数组中,但是这没有发生,而且我也看不出原因。

任何提示和有益的评论,我将不胜感激!

谢谢你!

0 个答案:

没有答案