I am a newbie in programming. Please explain so that a beginner can understand.
I'm trying to build a lottery. First I fill the array with the desired number of numbers. Then draw a number. The number is removed from the lottery and returned to main where it is printed on the console. This is repeated until all the numbers are drawn.
This is what I want. But I get the error "java.lang.NullPointerException".
Is my array empty? And if so, why?
import java.util.*;
public class Tombola {
private ArrayList<Integer> lottery;
private int numbers;
public Tombola(int n){
this.numbers = n;
ArrayList<Integer> lottery = new ArrayList<Integer>();
for(int i = 0; i < this.numbers; i++){
this.lottery.add(i+1);
}
}
public int draw(){
int drawnNumber = this.lottery.get((int) (Math.random() * numbers));
for(int i = 0; i < numbers; i++){
if(this.lottery.get(i) == drawnNumber){
this.lottery.remove(i);
break;
}
}
this.numbers--;
return drawnNumber;
}
public static void main(String[] args) {
int x = 10;
Tombola jackpot = new Tombola(x);
for(int i = 0; i < x; i++){
System.out.println(jackpot.draw());
}
}
}