I get a null pointer exception error when I run the method runSnackBar and I can't work out why. When I get this error BlueJ highlights the first line in the randomFlavour method but I have no idea why. Please help.
import java.util.Random;
import java.util.ArrayList;
public class SnackBar
{
private Random randomGenerator;
private String[] packets;
private SnackMachine newSnackMachine;
private ArrayList<Student> students;
public SnackBar(int numOfStudents, int numPackOfCrisps, int cost)
{
randomGenerator = new Random();
String[] packets = {"ready salted", "cheese and onion", "salt and vinegar" , "smokey bacon"};
newSnackMachine = new SnackMachine(numPackOfCrisps , cost);
for(int n=0 ; n < numPackOfCrisps ; n++){
newSnackMachine.addPack(new PackOfCrisps(randomFlavour()));
}
students = new ArrayList<Student>();
for(int i=0 ; i < numOfStudents ; i++){
students.add(new Student(randomFlavour(), newSnackMachine));
}
}
private String randomFlavour()
{
int index = randomGenerator.nextInt(packets.length);
return packets[index];
}
public void describe(){
System.out.println("The SnackBar has" + students.size() + "hungry students");
System.out.println("The SnackMachine has:" + newSnackMachine.countPacks("ready salted") + "packets of ready salted crisps");
System.out.println("," + newSnackMachine.countPacks("cheese and onion") + "of cheese and onion crisps");
System.out.println("," + newSnackMachine.countPacks("salt and vinegar") + "of salt and vinegar crisps");
System.out.println("," + newSnackMachine.countPacks("smokey bacon") + "of smokey bacon crisps");
}
public void runSnackBar(int nSteps){
for( int x=1 ; x < nSteps ; x++){
System.out.println("Time step" + x);
describe();
int y = randomGenerator.nextInt(students.size());
students.get(y).snackTime();
}
}
}