我为此获得了空指针异常,我不知道为什么
public static boolean hasPair(Card[] cards) {
int k=0;
cards = new Card[5];
for (int atPos = 0; atPos<5; atPos++){
for (int atPos2 = atPos+1; atPos2<5; atPos2++){
if(cards[atPos].getValue() == cards[atPos2].getValue()){
k++;
}
if (atPos2 == (cards.length-1) && k!=1){
k=0;
}
else if (atPos2 == (cards.length-1) && k>=2){
return true;
}
}
}
return false;
}
我的方法是测试我的手牌是否有两张持有相同值的牌,而nul指针表示它在这一行内
if(cards[atPos].getValue() == cards[atPos2].getValue()){
我也有这种方法......我可以用它作为助手吗?
public Card[] deal(int numCards) {
Card[] newArray;
newArray = new Card[numCards];
for (int index=0; index<numCards; index++){
newArray[index] = cards.get(0);
cards.remove(0);
}
return newArray;
}
答案 0 :(得分:3)
在第二行中,您将创建一个新的对象数组Card。该数组中的每个对象都为null,因此您需要先填充数组。