1

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];
}

}
1
  • It's a NullPointerException Commented Apr 9, 2015 at 19:27

2 Answers 2

2

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();
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You created your array of bingo cards, but you have initialized no item. That's why you get that null pointer exception. ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.