java - 为什么我会收到此错误消息,我该如何解决?
问题描述
我的代码可能看起来很奇怪。我刚开始学习。我想将“人”对象中的值存储在“icsClass”数组中。我不断收到此错误。
错误:com.company.Main.main(Main.java:74) 处的线程“main”java.lang.NullPointerException 中的异常
public static void main(String[] args) {
Student[] icsClass = new Student[MAX];
Scanner s = new Scanner(System.in);
int counter = 0;
int year;
int month;
int day;
System.out.println("This program will store 10 student's information.");
System.out.println("Do you want to continue? (Yes/No)");
String a = s.next();
if (a.equals("Yes") || a.equals("yes")) {
while (counter < MAX) {
Student person = new Student();
System.out.println("Please enter your first name:");
person.setfName(s.next());
System.out.println("Please enter your last name:");
person.setlName(s.next());
System.out.println("Please enter your year of birth:");
year = (s.nextInt());
while ((year < MIN_YEAR) || (year > MAX_YEAR)) {
System.out.println("Error: Year of birth must be between 1895 and 2021");
System.out.println("Please re-enter your year of birth:");
year = s.nextInt();
}
person.setYear(year);
System.out.println("Please enter your month of birth:");
month = (s.nextInt());
while ((month < MIN_MONTH) || (month > MAX_MONTH)) {
System.out.println("Error: Month of birth must be between 1 and 12");
System.out.println("Please re-enter your month of birth:");
month = s.nextInt();
}
person.setMonth(month);
System.out.println("Please enter your day of birth:");
day = s.nextInt();
while ((day < MIN_DAY) || (day > MAX_DAY)) {
System.out.println("Error: Day of birth must be between 1 and 31");
System.out.println("Please re-enter your day of birth:");
day = s.nextInt();
}
person.setDay(day);
counter = counter + 1;
if (counter == MAX) {
person.display();
System.out.println("Is the information above correct? (Yes/No)");
String b = s.next();
if ((Objects.equals(b, "Yes")) || (Objects.equals(b, "yes"))) {
icsClass[counter].setfName(person.getfName());
icsClass[counter].setlName(person.getlName());
icsClass[counter].setYear(person.getYear());
icsClass[counter].setDay(person.getDay());
icsClass[counter].setMonth(person.getMonth());
}
}
}
//for (int i = 0; i < MAX; i++) {
// icsClass[i].display();
//}
}
}
}
解决方案
推荐阅读
- google-play - Playstore 应用更新被拒绝 - 免责声明/来源:缺少明确的信息来源/免责声明
- python - python中的索引错误,请帮助修复
- scheme - 为什么我的结果中会出现“列表”一词?
- c++ - Visual Studio 2019 使用 32 位 FileTracker 和 64 位工具集
- java - 我应该如何实例化列表
- > 在 Java 中
- kotlin - 使用 Kotlin 的地图委托同步对可变字段的访问
- javascript - 为什么videojs在空格键上切换全屏,切换播放/暂停
- angular - Angular 2+ NGXS:状态在指令中未定义,尽管状态为默认值
- javascript - Javascript/JSP 遍历 Datatable 并计算发生次数
- sql - 如何通过替换各种文本的字符串来更新 sql 记录文本?