Reputation: 53
I have designed a data type called BingoCard which will create a random bingo card. I'm trying to make BingoCard into an array but I keep getting an error on this line: System.out.println(CurrentCard[i].toString());
I'm wondering if I've created the array correctly or am I doing something wrong? Thanks for any help in advance.
public class BingoGame {
private int[] counter;
private boolean done = false;
private int numOfCards;
private int fastestCard;
public BingoGame(int num){
numOfCards = num;
counter = new int[numOfCards];
}
public int play(){
for(int a=0;a<numOfCards;a++){
counter[a] = 0;
}
BingoCard[] CurrentCard = new BingoCard[numOfCards];
while(!done){
for(int i=0;i<numOfCards;i++){
System.out.println("This is the current card:");
System.out.println(CurrentCard[i].toString());
int currentNum = (int)(Math.random() * 75) + 1;
counter[i]++;
CurrentCard[i].currentNumber(currentNum);
CurrentCard[i].bingo();
if(CurrentCard[i].bingo()){
done = true;
}
fastestCard = i;
}
}
return counter[fastestCard];
}
}
Upvotes: 1
Views: 4232
Reputation: 426
You created your array of bingo cards, but you have initialized no item. That's why you get that null pointer exception. ;)
Upvotes: 0
Reputation: 159754
Elements in an Object
array are null
by default. You need to instantiate the elements before attempting to invoke any methods
for (int i = 0; i < numOfCards; i++) {
currentCard[i] = new BingoCard();
...
}
Upvotes: 2