John314
John314

Reputation: 7

Initializing array of objects to use with method

Ok so the relevant part of code:

 public static void assignClassBig(Schedule[] bigSchedule, Student student, int classA, ArrayList<Integer>[] classesXperiods, ArrayList<Integer> classes, int period) {
    int id = student.getID();
    int classB = classes.get(classA);
    int periodA = classesXperiods[classB].get(period) + 1;
    bigSchedule[id].assignClass(classB, periodA);
  }

bigSchedule is an array of the object Schedule which contains a student object and an array which acts as the schedule, each entry in the array is a period etc.

Now in my main method I made the array:

Schedule[] bigSchedule = new Schedule[nOstudents];

However, when I try to access one of the Schedule objects in it say Schedule[0] it is a null value. How do i initialize the schedules or whatever it is called so I can use it.

Upvotes: 0

Views: 26

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198043

You have to explicitly set it:

for (int i = 0; i < nOstudents; i++) {
  bigSchedule[i] = new Schedule(); // or however you construct Schedules
}

Upvotes: 3

Related Questions