BlackJack Java:异常错误

时间:2013-06-28 15:12:57

标签: java runtime-error blackjack

我的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;
}

3 个答案:

答案 0 :(得分:3)

您正在传递null数组

this.createDeck(numCards, 4, null);

稍后在您的声明

中将其作为cardRanks[j-1]进行访问
deck.add(new Card(new Suit(i), cardRanks[j-1], cardRanks, j));

编辑
您可能需要的是将Deck创建为

this.createDeck(numCards, 4);

并将Card添加为

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

因为String[] cardRanks已经在您的Card课程中初始化了。并且,从设计角度看它是正确的,因为Deck不应该知道卡片等级。 Card应封装它,也可能实现Comparable

答案 1 :(得分: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,即不存在,则它无法访问内部,NullPointerException是唯一的选项。 Java可以理解地抱怨。

在英语中,你告诉程序要做的事情(当cardRanks为null时)是采用'nothing'的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;

    }

}

答案 2 :(得分:0)

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

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

this.createDeck(numCards, 4, null);

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