我在将对象添加到 Java 中的 arrayList 时遇到问题。运行代码时出现以下错误。这是我的两个文件的片段。如果有人指出我的错误,我将非常感激。谢谢,乔
java.lang.NullPointerException at House.addRoom(House.java:18)at House.(House.java:36)
//房间类
public Room () {
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");
System.out.println("Enter description of room:");
description = scan.next();
System.out.println("Enter length of room:");
length = scan.nextDouble();
System.out.println("Enter width of room:");
width = scan.nextDouble();
}
//家庭类
public class House {
private static ArrayList<Room> abode;
public void addRoom (){
abode.add(new Room ());
}
public House () {
idNum = ++internalCount;
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");
System.out.println("Enter address of house:");
address = scan.next();
System.out.println("Enter number of rooms:");
numRooms = scan.nextInt();
System.out.println("Enter type of house:");
houseType = scan.next();
for (int i=1; i<=numRooms; i++){
addRoom();
}
}
}