我對這裏的情況非常難過。 我真的不知道爲什麼我得到這個空指針異常。用戶在java中輸入後的NullPointerException
//這裏是一個方法,從我的課之一
public class UI {
Registry reg;
public UI (Registry reg) {
this.reg = reg;
menu();
}
private void addAppts(Scanner sc) {
System.out.println("Enter the month for appointment in mm/dd/yyyy format:");
sc.nextLine();
int month = sc.nextInt();
System.out.println("Enter the day for appointment mm/dd/yyyy format:");
sc.nextLine();
int day = sc.nextInt();
System.out.println("Enter the year for appointment mm/dd/yyyy format:");
sc.nextLine();
int year = sc.nextInt();
System.out.println("Enter the hour for appointment:");
sc.nextLine();
int hour = sc.nextInt();
System.out.println("Enter the minute for appointment:");
sc.nextLine();
int minute = sc.nextInt();
System.out.println("Enter the owner name for appointment:");
sc.nextLine();
String ownerName = sc.nextLine();
reg.addAppts(month, day, year, hour, minute, ownerName);
}
}
//這裏是從我的其他類的另一種方法
public class Registry {
private Appointments[] appts;
public void addAppts(int month, int day, int year, int hour, int minute, String ownerName) {
if (next < appts.length) {
Appointments e = new Appointments(month, day, year, hour, minute, ownerName);
appts[next++] = e;
}
}
}
public class Appointments {
private int month, day, year, hour, minute;
private String ownerName;
public Appointments(int month, int day, int year, int hour, int minute, String ownerName) {
this.month = month;
this.day = day;
this.year = year;
this.hour = hour;
this.minute = minute;
this.ownerName = ownerName;
}
}
我越來越對的空指針異常錯誤第一種方法的最後一行。 有什麼建議嗎?
'reg'定義在哪裏?它初始化了嗎? – sanbhat
「UI」如何被屏蔽? – epoch
向我們展示堆棧跟蹤,以及在哪裏啓動'appts',我認爲這可能是一個問題,如果這是完整的代碼 – user902383