Getting a NullPointerError
on line 24 (second for loop) and an arrayoutofindexexception
on the main method. I have no idea how to solve this and an explanation would be much appreciated. Please help!
Edit: 'NullPointerError' solved, just needed to initialize the ArrayList for it, but still can't work out the 'ArrayOutOfIndexException' error.
import java.util.*;
public class SnackBar
{
private Random randomizer;
private String[] flavours;
private SnackMachine barMachine;
ArrayList<Student> students;
public SnackBar(int numStudents, int packetsBarMachine, int costPacket)
{
randomizer = new Random();
flavours = new String[] {"prawn cocktail", "tango cheese", "natural", "paprika", "salt and vinegar"};
barMachine = new SnackMachine(packetsBarMachine, costPacket);
students = new ArrayList<Student>();
for(int i = 0; i < packetsBarMachine; i++)
{
barMachine.addPack(new PackOfCrisps(randomFlavour()));
i++;
}
for(int j = 0; j < numStudents; j++)
{
students.add(new Student(randomFlavour(), barMachine));
j++;
}
}
public String randomFlavour()
{
int random = randomizer.nextInt(flavours.length);
return flavours[random];
}
public void describe()
{
System.out.println("The SnackBar has " + students.size() + " hungry students.");
System.out.println("The Snackmachine has:");
System.out.println(barMachine.countPacks("prawn cocktail") + " packets of prawn cocktail crisps,");
System.out.println(barMachine.countPacks("tango cheese") + " packets of tango cheese crisps,");
System.out.println(barMachine.countPacks("natural") + " packets of natural crisps,");
System.out.println(barMachine.countPacks("paprika") + " packets of paprika crisps,");
System.out.println(barMachine.countPacks("salt and vinegar") + " packets of salt and vinegar crisps.");
}
public void runSnackBar(int nSteps)
{
int step = 1;
while(step <= nSteps)
{
System.out.println("Time Step " + step);
describe();
int atRandom = randomizer.nextInt(students.size());
students.get(atRandom).snackTime();
step++;
}
}
public static void main(String[] args)
{
SnackBar bar = new SnackBar (Integer.parseInt(args[4]), Integer.parseInt(args[20]), Integer.parseInt(args[5]));
bar.runSnackBar(Integer.parseInt(args[30]));
}
}