Java의 arrayList에 객체를 추가하는 데 문제가 있습니다. 내 코드를 실행할 때 다음과 같은 오류가 발생합니다. 이것은 내 파일 두 조각입니다. 나는 내 오류를 지적 할 사람이 많이 있었을 것이다. 감사합니다, 조arrayList에 객체 추가
java.lang.NullPointerException이 House.addRoom 하우스 (House.java:18). (House.java:36)에서
// 룸 CLASS
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();
}
/이 당신의 arraylist
를 초기화하는 요소를 추가하기 전에/하우스 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
@gontard 내 편집을 확인 했습니까 ?? :) – PermGenError
nth edit :)? 아니. – gontard