Skip to content
Advertisement

Getter, Setter & NullPointerException

Firstly, I am trying to assign the value for array I initialized locally. The class type of the array is stored inside another class and the variable is private so I am using getter and setter to set the value. But it showing “Exception in thread “main” java.lang.NullPointerException at room.Test.main(Test.java:26)”, below is my code for the test.java:

JavaScript

Below is the class I used to store the Room type:

JavaScript

PS. The records stored inside my Room.txt is like

JavaScript

Advertisement

Answer

You need to write room[I] = new Room(); before you start calling its setters.

Advertisement