-2

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();
        }


    }
}
mounaim
  • 1,132
  • 7
  • 29
  • 56

2 Answers2

3
private String[] packets;

packets is a assigned null by default, and here

String[] packets = {"ready salted", "cheese and onion", 
                    "salt and vinegar" , "smokey bacon"};

you're declaring a local variable. So you're getting NPE here:

int index = randomGenerator.nextInt(packets.length);
                                    ↑ 
                                   null

JLS 4.12.5. Initial Values of Variables:

For all reference types (§4.3), the default value is null.

ArrayType is a ReferejceType

Maroun
  • 94,125
  • 30
  • 188
  • 241
1

You attempted to initialize packets in the constructor, but you accidentally created a local variable.

Change

String[] packets = {"ready salted", "cheese and onion", "salt and vinegar" , "smokey bacon"};

to

packets = new String[] {"ready salted", "cheese and onion", "salt and vinegar" , "smokey bacon"};

so packets refers to your instance variable, so it gets initialized properly.

rgettman
  • 176,041
  • 30
  • 275
  • 357