1

我的 BlackJack Java 游戏出现错误。错误是:

Exception in thread "main" java.lang.NullPointerException
at com.keegan.game.blackjack.Deck.createDeck(Deck.java:48)
at com.keegan.game.blackjack.Deck.<init>(Deck.java:33)

而且我对这段代码中的问题一无所知。谁能帮我理解错误告诉我的错误是什么?

这是代码Deck.java

public class Deck {

private ArrayList<Card> deck = new ArrayList<Card>();

private List<Card> cardUsed = new ArrayList<Card>();
Card c = new Card(null, null, null, 0);

public Deck(int numCards) {
    this.createDeck(numCards, 4, null);
}

private void createDeck(int numCards, int numSuits, String[] cardRanks) {
    deck = new ArrayList<Card>();
    cardUsed = new ArrayList<Card>();
    if ((numCards % numSuits) > 0) return;
    for (int i=0; i < numSuits; i++) {
        for(int j=1; j <= (numCards / numSuits); j++) {
            deck.add(new Card(new Suit(i), cardRanks[j-1], cardRanks, j));
        }
    }
}

public Card dealCard( ) {

    Card dealtCard = null;
    if (deck.size() == 0){
        deck.addAll(cardUsed);
        this.shuffle();
        cardUsed = new ArrayList<Card>();
    }

    dealtCard = deck.get(0);
    deck.remove(0);
    cardUsed.add(dealtCard);

    return dealtCard;
}

public void shuffle() {
    Collections.shuffle(deck);
}

public ArrayList<Card> getDeck() {
    return deck;
}

public void setDeck(ArrayList<Card> deck) {
    this.deck = deck;
}
public int getNumUsedCards() {
    return cardUsed.size();
}

public List<Card> getCardUsed() {
    return cardUsed;
}

public void setCardUsed(List<Card> cardUsed) {
    this.cardUsed = cardUsed;
}
4

3 回答 3

3

你正在传递一个null数组

this.createDeck(numCards, 4, null);

然后cardRanks[j-1]在您的声明中访问它

deck.add(new Card(new Suit(i), cardRanks[j-1], cardRanks, j));

编辑
您可能需要的是创建您的Deckas

this.createDeck(numCards, 4);

并添加Cardas

deck.add(new Card(new Suit(i), j));

因为,String[] cardRanks已经在你的Card类中初始化了。而且,从设计上讲,它是正确的,因为Deck不必了解卡牌等级。Card应该封装它,也可能实现 Comparable

于 2013-06-28T15:15:26.657 回答
1

在您的构造函数中,您已将 null 作为变量传递

public Deck(int numCards) {
    this.createDeck(numCards, 4, null);
}

在您的方法 createDeck 然后您尝试使用该变量

private void createDeck(int numCards, int numSuits, String[] cardRanks) { //<---cardRanks is null at this point
    deck = new ArrayList<Card>();
    cardUsed = new ArrayList<Card>();
    if ((numCards % numSuits) > 0) return;
    for (int i=0; i < numSuits; i++) {
        for(int j=1; j <= (numCards / numSuits); j++) {
            deck.add(new Card(new Suit(i), cardRanks[j-1], cardRanks, j)); 
        }
    }
}

当您编写时,cardRanks[j-1]您正试图访问 cardRanks 的“内部”。如果cardRanks 为null,即不存在,则它无法访问内部,aNullPointerException是它唯一的选择。Java 抱怨是可以理解的。

在英语中,您告诉程序要做的事情(当 cardRanks 为空时)是采用“无”的 j-1 元素。这显然是没有意义的,这NullPointerException是编译器告诉你的


创建不为空的字符串数组

String[] stringArray=new String[10]; //creates an array big enough to hold 10 Strings

目前 stringArray 包含 10 个空值

stringArray[0]="hi"; //enter first element
stringArray[1]="hi again"; //enter second element
stringArray[2]=""; //empty string is not the same as null

现在前 3 个元素不为空,其余 7 个保持为空

您当然可以在循环中或以您认为合适的任何方式创建字符串

创建一个不需要在构造函数中获取其字段的类

public class SomeClass {
    String[] someStringArray={"hello", "i'm just fine as I am"}; //creates an array of strings, 2 entries

    int someNumber;

    public SomeClass(int someNumber){
        //this constructor leaves someStringArray alone, its fine as it is
        this.someNumber=someNumber;

    }

    public SomeClass(int someNumber,String[] someStringArray){
        //this constructor changes someStringArray
        this.someNumber=someNumber;
        this.someStringArray=someStringArray;

    }

}
于 2013-06-28T15:16:39.707 回答
0
deck.add(new Card(new Suit(i), cardRanks[j-1], cardRanks, j));

在这一行中,cardRanks 为空。正如您将方法称为

this.createDeck(numCards, 4, null);

您无法访问空数组的元素 (:p)

于 2013-06-28T15:17:40.900 回答