我在將對象添加到Java中的arrayList時遇到問題。當我運行我的代碼時,出現以下錯誤。這是我的兩個文件的片段。如果有人指出我的錯誤,我會非常感激。 謝謝,喬將對象添加到arrayList
顯示java.lang.NullPointerException在House.addRoom(House.java:18)在房屋。(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();
}
// HOUSE CLASS
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();
}
}
}
-1您是否嘗試編譯您的答案? – gontard 2013-03-14 10:49:35
@gontard你檢查我的編輯?? :) – PermGenError 2013-03-14 10:49:48
你的第n編輯:)?沒有。 – gontard 2013-03-14 10:50:59