-4

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

G_S
  • 49
  • 4

5 Answers5

1

If you want 50 unique rooms to exist, the code new Room() is going to have to run 50 times, one way or another. Room[] r = new Room[50]; just creates 50 slots in which you can write the location of a room, it does not create any rooms. new Room() creates a room, and nothing else will.

for (int i = 0; i < rooms.length; i++) {
    rooms[i] = new Room();
}

Now you create 50 new rooms and assign them to a slot in your 'list of locations of rooms'.

Remember, ALL java types, except primitives, are references, pointers, treasure maps (and not treasure), locations of rooms (and not rooms), etcetera – whichever way of thinking is easier for you.

Because you didn't create the rooms in your example, e.g. room[5] is a null pointer (room[5] can hold a location to a room, but when you ran that code, there was a blank space instead, thus, when you ask somebody to 'walk to the room written down here and write empty on the blackboard you find inside', they're not going to know what that even means, if you're pointing at a blank space where a location would ordinarily be written. In java-ese: A NullPointerException occurs.

rzwitserloot
  • 65,603
  • 5
  • 38
  • 52
0

This Room[] rooms = new Room[8]; just creates an array of type Room. It will not create an array containing 8 Room objects. Also, the rooms array inside School is not being used. So instead of a static method, initialize the rooms array in the School object using a constructor.

You need to update the School class as:

public class School {

    private Room[] rooms;

    public School(int length) {
        rooms = new Room[length];
        for (int x = 0; x < length; x++) {
            Room r = new Room(); // create a room object and add to array.
            r.setName("empty");
            rooms[x] = r;
        }
        System.out.println(Arrays.toString(rooms));
    }
}

And in the main method create a School object : School s = new School(8)

Gautham M
  • 4,603
  • 3
  • 13
  • 33
0

Create structure like this. Modify the code as you need.

public class Test {

        public static void main(String[] args) throws Exception {
            Teacher t1 = new Teacher("John");
            Classroom cr = new Classroom("5thA");
            cr.setTeacher(t1);
            School school = new School("ABC School");
            school.addClassRoom(cr);
    
            System.out.println(school.getClassRooms());
        }
    }
    
    class Teacher {
        private String name;
        public Teacher(String name) {
            this.name = name;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
    }
    
    class Classroom {
        private String name;
        private Teacher teacher;
        public Classroom(String no) {
            this.name = no;
        }
        
        public void setTeacher(Teacher teacher) {
            this.teacher = teacher;
        }
        
        public Teacher getTeacher() {
            return this.teacher;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
    }
    
    class School {
        String name;
        List<Classroom> classRooms = new ArrayList<>();
        
        public School(String name) {
            this.name = name;
        }
        
        public String getName() {
            return this.name;
        }
        
        public void addClassRoom(Classroom cr) {
            classRooms.add(cr);
        }
        //write methods for remove class room
        
        public List<Classroom> getClassRooms(){
            return this.classRooms;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
    }

Note: Put these classes in separate files.

the Hutt
  • 15,770
  • 2
  • 9
  • 36
0

methods initialise:

public static void initialise(Room[] schoolRef) {
    for (int x = 0; x < schoolRef.length; x++) {
        schoolRef[x] = new Room();
        schoolRef[x].setName("empty");
        System.out.println(Arrays.toString(schoolRef));
    }
}

String do note need instantiation( new Object).Because java did it. but if you want use a Object you should new Object first.

Gautham M
  • 4,603
  • 3
  • 13
  • 33
0

I can see 3 issues in your code.

  1. You have created array if School and not initialised object. Before schoolRef[x].setName("empty"); line add schoolRef[x] = new Room();

  2. Calling Arrays.toString(schoolRef) will expect Object's toString method to be overridden in your Room class. Otherwise it will print reference of object and not actual value. Added below to Room class public String toString() { return " " + name; }

  3. Write System.out.println(Arrays.toString(schoolRef)); out of the loop