我意識到已經有一堆這樣的問題,但到目前爲止我看到的每個問題都是ArrayList未初始化。我想我的初始化,但我試圖添加到列表中時,我得到一個NullPointer。這裏是我的代碼:NullPointer添加到ArrayList時
import java.util.ArrayList;
import java.util.Random;
import java.util.Collections;
public class Deck {
static final int DECKSIZE = 48;
static final int MAX = 10;
static final int MIN = 1;
public ArrayList<Card> cardDeck = new ArrayList<Card>();
Random rand;
public Deck() {
initializeDeck();
}
private void initializeDeck() {
for (int i=0; i<DECKSIZE; i++) {
cardDeck.add(i, new Card("Name" + (i+1), "Race" + (i+1), "Orientation" + (i+1)));
}
}
public void shuffleCards() {
Collections.shuffle(cardDeck);
}
}
下面是堆棧跟蹤告訴我initializeDeck是一個空指針
Caused by: java.lang.NullPointerException
at hr.krypto.cardwars.deck.Deck.initializeDeck(Deck.java:24)
at hr.krypto.cardwars.deck.Deck.<init>(Deck.java:15)
at hr.krypto.cardwars.deck.Dealer.<init>(Dealer.java:14)
at hr.krypto.cardwars.model.CardTable.<init>(CardTable.java:15)
at hr.krypto.cardwars.screen.GameScreen.show(GameScreen.java:31)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at hr.krypto.cardwars.CardWarsGame.create(CardWarsGame.java:19)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:127)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)
請發佈堆棧跟蹤 – rajesh 2013-03-27 10:43:58
stacktrace應該指出你有問題的地方。 – Howard 2013-03-27 10:44:04
@Keyser此方法只能拋出'IndexOutOfBoundsException' – 2013-03-27 10:46:17