我不确定我在这里做了什么,但我尝试使用java中的LinkedList集合来创建一副扑克牌。我的逻辑中似乎有错误。我一直在甲板构造函数中得到一个NullPointerException。
我正在尝试重新编写使用数组的代码来代替使用LikedList。我错过了什么?
public class Deck {
/**
* A LinkedList of cards in the deck, where the top card is the
* first index.
*/
private LinkedList<Card> mCards;
/**
* Number of cards currently in the deck
*/
private int numCards;
/**No args Constructor- if no arguments are used when creating a deck,
* then we define the game deck to be one deck without shuffling.
*
*/
public Deck(){ this(1, false); }
/**Constructor that defines the number of decks (how many sets of 52
* cards are in the deck) and whether it should be shuffled.
*
* @param numDecks the number of individual decks in this game deck
* @param isShuffled whether to shuffle the cards
*/
public Deck(int numDecks, boolean isShuffled){
this.numCards = numDecks * 52;
//for each deck
for (int i = 0; i < numDecks; i++){
//for each suit
for(int j = 0; j < 4; j++){
//for each number
for(int k = 1; k <= 13; k++){
//add card to the deck
this.mCards.add(new Card(Suit.values()[j], k));
}
}
}
if(isShuffled){
this.shuffle();
}
public enum Suit {
Clubs,
Diamonds,
Spades,
Hearts,
}
答案 0 :(得分:4)
您永远不会初始化mCards
。
添加(在构造函数的开头):
mCards = new LinkedList<Card>();