我意识到已经有很多这样的问题,但到目前为止我看到的每个问题都是 ArrayList 没有初始化。我认为我的已初始化,但在尝试添加到列表时我得到一个 NullPointer。这是我的代码:
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 是一个 nullPointer
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)