所以我试图测试我的deckOfCards数组是否打算正确打印出来。这是代码
public class Deck {
private static final String face[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private static final String suit[] = {"Spade", "Club", "Diamond", "Heart"};
private static final Card deckOfCards[] = new Card [52];
public Deck(){
int suitCount = 0;
int faceCount = 0;
int faceValue = 0;
for(int i = 1; i < deckOfCards.length; i++){
deckOfCards[i].setSuit(suit[suitCount]);
deckOfCards[i].setFace(face[faceCount], faceValue);
suitCount++;
faceCount++;
faceValue++;
if(suitCount == 3){
suitCount = 0;
}
if(faceCount == 12){
faceCount = 0;
}
if(faceValue == 13){
faceValue = 1;
}
}
}
正如您所看到的,我已经定义了数组中的face和suit,并且我想将它们分配给它们正确的值,但是当我尝试运行它时,我在行{{1}处得到nullPointerException }}
我似乎无法弄清楚为什么它给我这个错误,当我调试它时,它似乎初始化所有字段,但当它到达那里它崩溃。
答案 0 :(得分:2)
您初始化数组,但不是其中的元素。你可以在循环中做到这一点:
deckOfCards[i] = new Card();
deckOfCards[i].setSuit(suit[suitCount]);