2

I have a Building class with a rooms property of type List:

private List<Room> rooms;

I wrote the following method to add an room to the list:

public void addRoom(Room room) {
        this.rooms.add(room);
    }

This code is not raising an exception immediately, but the room is not added to list. Also, the method is called in an iteration, after returning from addRoom method to this iteration, the next iterations are skipped and then an NullPointerException is raised.

I'm creating Room objects from a file. Here's how I call the method:

Room newRoom = new Room();
newRoom.setName(meetingRoom[0]);
newRoom.setCode(meetingRoom[1]);
few more setters ...
home.addRoom(newRoom);

home is an instance of Building found by another method earlier, its value is ok according to the debugger.

Spasitel
  • 169
  • 7

1 Answers1

5

Is your List ever instantiated?

Try with

private List<Room> rooms = new LinkedList<>();
nitowa
  • 1,031
  • 1
  • 9
  • 21