首页 > 解决方案 > 为什么我会收到此错误消息,我该如何解决?

问题描述

我的代码可能看起来很奇怪。我刚开始学习。我想将“人”对象中的值存储在“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();
            //}
        }
    }
}

标签: javaarrays

解决方案


推荐阅读