Currently I have a code like this The School array represents the School and each element in the array represents a classroom At the moment I want to store the Class teachers name in the array and if there is no class teacher assigned yet, that particular array element has "empty"
import java.util.*;
public class Main
{
public static void main(String[] args) {
String[] school = new String[8];
initialise(school);
}
private static void initialise( String schoolRef[] ) {
for (int x = 0; x < schoolRef.length; x++ ) schoolRef[x] = "empty";
System.out.println(Arrays.toString(schoolRef));
}
}
Output of this is
[empty, empty, empty, empty, empty, empty, empty, empty]
However now I want to create a Class called School and a Class called Classroom I want to use an array of Classroom objects
School.java
import java.util.Arrays;
public class School {
private Room[] rooms;
public static void initialise(Room[] schoolRef) {
for (int x = 0; x < schoolRef.length; x++) {
schoolRef[x].setName("empty");
System.out.println(Arrays.toString(schoolRef));
}
}
}
Room.java
public class Room {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}
Main.java
public class Main {
public static void main(String[] args) {
Room[] rooms = new Room[8];
School.initialise(rooms);
}
}
However when I run the Main method I get a NullPointerException. I want to create an array of Classroom objects instead of writing everything in one Class and I'm not sure what I am doing wrong in my approach. Appreciate any help.Thanks