(java初学者)我不知道如何克服空错误

时间:2020-05-03 16:54:23

标签: java nullpointerexception

工作表达 雅典的一所幼儿园设有婴幼儿班,希望创建一个应用程序来保存 每学年学生的历史记录。 系统支持以下功能:

  1. 打印所有已参加或已参加该站的学生

  2. 当前学年的新学生注册

  3. 从当前学年删除一名学生
  4. 根据用户设置的定义搜索和打印班级(学生和教师) 班级(例如婴儿)和学习年份(例如“ 2017-2018”)
  5. 搜索每位学校老师上的课
  6. 通过AMKA搜索教师数据

建模

每个学生的描述如下: 唯一代码(id)

全名

出生日期

创建的对象存储在一个静态类型表Student 100的座位中,可供调用。

每位老师的描述如下: 唯一代码(ID) 全名 安卡

创建的对象存储在教师100个座位的静态类型表中,可供调用。

每个学年的描述如下:

学习年份,例如“ 2019-2020”

类型表学生在每个位置还存储了一个学生 (学生类型变量)今年正在学前班。 (此表 不是 上面提到的静态表。这是一个可变的快照,因为它描述了学龄前儿童的班级 每个学年) 和学龄前班的老师(Teacher type variable) 以及在每个位置存储学生的Type表Student (学生类型变量)今年正在婴儿班学习。 (此表是静态的 上文提到的。这是一个可变的快照,因为它描述了每所学校的婴儿类别 年) 和婴儿班老师(Teacher type variable) 表格的大小和学生人数可能会每年变化,但可能不会变化 在一年中改变。这意味着当创建一个学年时,这些表格的大小 必须设置。学生不必完成课程,但在任何情况下都不能 每个班级都要研究大于相应表大小的数字。这就是为什么 我们将每个学生存储在表格中的可用位置(空缺表示其内容为空) 创建的对象存储在静态类型表SchoolYear中 100个席位可供召回。 功能性 该程序包括上一节中描述的3个类和一个负责 启动并运行包含主方法的程序,并打印以下选项 菜单:

1. Print all students who have attended school
2. Registration of a new student in the current school year
3. Delete a student from the current school year
4. Search for school year data
5. Search for classes taken by each school teacher
6. Search for teacher information via AMKA 


I have a feeling that this error comes from the Teacher "empty" constructor i made in order to create object3. If that's so how am i supposed to create an object of the class Teacher without using the parameters of the other constructor i've made? Since by creating an object3, i don't want to create an actual teacher, but to use it for other purposes such as calling Teacher methods etc. I'm sorry if i frustrated you but I am a beginner!!
Thank you for your time!

the error i get is:

        Exception in thread "main" java.lang.ExceptionInInitializerError
            at Ergasia.YearBook.main(YearBook.java:20)
        Caused by: java.lang.NullPointerException
            at Ergasia.Teacher.<init>(Teacher.java:19)
            at Ergasia.Teacher.<clinit>(Teacher.java:13)
            ... 1 more


        ```public class Teacher {```

        private int teacherId, teacherAmka,teacherCounter=0;
        private String teacherName;
        static int count=0; 

        int teacherIds[] = new int[100];
        String teacherNames[] = new String[100];
        int teacherAmkas[] = new int[100];

        static Teacher teacherPronipio2018_2019 = new Teacher("George Mill", 565637);
        static Teacher teacherNipio = new Teacher("John Snow", 564319);
        static Teacher teacherPronipio2019_2020 = new Teacher("Jacob Jonson", 564459);

        private static Teacher teacherData[]= new Teacher[100];{

        teacherData[0] = teacherPronipio2018_2019;
        teacherData[1] = teacherNipio;
        teacherData[2] = teacherPronipio2019_2020;}



        public Teacher getTeacherData(int i) {
            return teacherData[i];

        }
        public Teacher[] getTeacherData() {
            return teacherData;
        }


        public Teacher(String teacherName, int teacherAmka) {

            teacherData[count]=this;
            count++;
            this.teacherId = count;
            this.teacherName = teacherName;
            teacherAmkas[teacherCounter++] = teacherAmka;

        }


        public Teacher() {

        }
        public String toString() {
            return teacherId + " " + teacherName + " " + teacherAmka;
        }

        public String getTeacherName(Teacher teacherData) {
            return teacherName;
        }

        public int getAmkaNumber(int i) {
            return teacherAmkas[i];
        }

    }


and the class with main:



    ```public class YearBook {```

        public static void main(String[] args) {

            Scanner sc = new Scanner(System.in);
            int choice;
            int choice2,amkaNumber;
            int studentId,yearOfBirth;
            String studentName,answer;
            boolean flag = false;


            Student object2 = new Student();
            Student studentData[] = object2.getStudentData();

            Teacher object3 = new Teacher();

            Teacher teacherData[] = object3.getTeacherData();
            Teacher teacherPronipio2018_2019 = teacherData[0];
            Teacher teacherNipio = teacherData[1];
            Teacher teacherPronipio2019_2020 = teacherData[2];

            SchoolYear schoolYearObject = new SchoolYear();
            SchoolYear years[] = schoolYearObject.years; 

            Student[] pronipio2018_2019 = new Student[100];
            Student[] pronipio2019_2020 = new Student[100];
            Student[] nipio2018_2019 = new Student[100];
            Student[] nipio2019_2020 = new Student[100];

            for (int c = 0; c<100 ; c++) {
                if (schoolYearObject.schoolYear2018_2019.getPronipio(c) != null) {
                pronipio2018_2019[c]=schoolYearObject.schoolYear2018_2019.getPronipio(c);
                }
                if (schoolYearObject.schoolYear2018_2019.getNipio(c)!= null) {
                    nipio2018_2019[c] =  schoolYearObject.schoolYear2018_2019.getNipio(c);
                }
                if (schoolYearObject.schoolYear2019_2020.getPronipio(c) != null) {
                    pronipio2019_2020[c]=schoolYearObject.schoolYear2019_2020.getPronipio(c);
                }
                if (schoolYearObject.schoolYear2018_2019.getNipio(c)!= null) {
                    nipio2019_2020[c] =  schoolYearObject.schoolYear2019_2020.getNipio(c);
                }
            }


            do {

            System.out.println("Menu:");
            System.out.println("1. Show all the Students");
            System.out.println("2. Register a new Student in current School year");
            System.out.println("3. Delete a Student from current School year");
            System.out.println("4. Search Data for current School year");
            System.out.println("5. Search every teacher's class");
            System.out.println("6. Search teacher by AMKA number");
            System.out.println("7.Exit");

            do {
                System.out.print("Please pick one of the choices (1-6): ");
                choice = sc.nextInt();
                if (choice > 6 || choice < 1) {
                    System.out.println("The number you gave is invalid.");
                }
            }while (choice > 7 || choice < 1);

            if (choice == 1) {
                int i = 0;
                while (i <= 100) {
                    if (studentData[i] != null){ 
                    System.out.println(studentData[i].toString());
                    i++;
                    }
                }

            }else if (choice == 2) {
                System.out.println("Would you like to Register the student\nat pronipio or Nipio? ");
                System.out.print("Press 1 for Pronipio and 2 for Nipio: ");
                choice2 = sc.nextInt();
                while (choice2 != 1 && choice2 != 2) {
                    System.out.println("The number you gave is invalid.");
                    choice2 = sc.nextInt();
                } 

                if (choice2 == 1) {
                    int i=0;
                        while ( i < 100 && flag == false) {
                            if (pronipio2019_2020[i] == null) {
                                flag = true;
                                System.out.println("Please Give Name and Year of Birth: ");
                                studentName = sc.nextLine();
                                yearOfBirth = sc.nextInt();
                                Student newStudent = new Student(studentName,yearOfBirth);
                                pronipio2019_2020[i] = newStudent;
                                i++;
                                int j=0;
                                while(j < 100 && flag == false) {
                                    if (studentData[j] == null ) {
                                        studentData[i] = newStudent;
                                        j++;
                                    }
                                }
                            }
                        }   
                }else {
                    int i=0;
                    while ( i < 100 && flag == false) {
                        if (nipio2019_2020[i] == null) {
                            flag = true;
                            System.out.println("Please Give 6-digit student Id,\nName and Year of Birth: ");
                            studentId = sc.nextInt();
                            studentName = sc.nextLine();
                            yearOfBirth = sc.nextInt();
                            Student newStudent = new Student(studentName,yearOfBirth);
                            nipio2019_2020[i] = newStudent;
                            i++;
                            int j=0;
                            while(j < 100 && flag == false) {
                                if (studentData[j] == null ) {
                                    studentData[i] = newStudent;
                                    j++;
                                }
                            }
                        }
                    }

                }
            }else if(choice == 3) {
                System.out.println("Please Give the 6-digit Id of the Student You Would Like to Delete: ");
                studentId = sc.nextInt();
                int i =0;
                flag = false;
                while (i < 100 && flag == false) {
                    if (object2.getStudentId(pronipio2019_2020[i]) == studentId) {
                        flag = true;
                        System.out.print("Are you sure you want to delete this student? Yes or No: ");
                            answer = sc.nextLine();
                            if(answer == "yes" || answer == "Yes") {
                                pronipio2019_2020[i] = null;
                            }else if(answer == "no" || answer == "No"){
                                System.out.println("OK");
                            }
                    }
                }
                if (flag == false){
                    while (i < 100 && flag == false) {
                        if (object2.getStudentId(nipio2019_2020[i]) == studentId) {
                            flag = true;
                            System.out.print("Are you sure you want to delete this student? Yes or No: ");
                                answer = sc.nextLine();
                                if(answer == "yes" || answer == "Yes") {
                                    nipio2019_2020[i] = null;
                                }else if(answer == "no" || answer == "No"){
                                    System.out.println("OK");
                                }
                        }
                    }
                }

            }else if(choice == 4) {
                flag = false;
                do {
                System.out.print("Please Pick a School Year (2018-2019 or 2019-2020): ");
                answer = sc.nextLine();
                if (answer == "2018-2019") {
                    flag = true;
                    System.out.print("Please Pick 1 for pronipio and 2 for nipio: ");
                    choice2 = sc.nextInt();
                    if (choice2 == 1) {
                        System.out.println(object3.getTeacherName(teacherPronipio2018_2019));
                    }else if (choice2 == 2) {
                        System.out.println(object3.getTeacherName(teacherNipio));
                    } 

                }else if (answer == "2019-2020") {
                    flag = true;
                    System.out.print("Please Pick 1 for pronipio and 2 for nipio: ");
                    choice2 = sc.nextInt();
                    if (choice2 == 1) {
                        System.out.println(object3.getTeacherName(teacherPronipio2019_2020));
                    }else if (choice2 == 2) {
                        System.out.println(object3.getTeacherName(teacherNipio));
                    } 
                }else
                    System.out.println("There are no data for the year you searched for");

                }while (flag == false);

            }else if(choice == 5) {
                for(int i = 0 ; i <= 2 ; i++) {
                    for(int j = 0 ; j < 4  ; j++) {
                        if(j==0 || j==1) {
                            int c1 = 0;
                            for(int c2=0 ; c2 <= 1 ; c2++ ) {
                                if(teacherData[i] == schoolYearObject.getTeacher(years[c1], c2) && teacherData[i]!=null) {
                                    System.out.print(teacherData[i] + " 2018-2019 ");
                                    if (i==0) {
                                        System.out.println("Pronipio");
                                    }else if(i==1) {
                                        System.out.println("Nipio");
                                    }
                                }
                            }

                        }else if(j==2 || j==3) {
                            int c1 = 0;
                            for(int c2=2 ; c2 >= 1 ; c2-- ) {
                                if(teacherData[i] == schoolYearObject.getTeacher(years[c1], c2) && teacherData[i] != null) {
                                    System.out.print(teacherData[i] + " 2019-2020 ");
                                    if (i==2) {
                                        System.out.println("Pronipio");
                                    }else if(i==1) {
                                        System.out.println("Nipio");
                                    }
                                }
                            }

                        }
                    }
                }

            }else if(choice == 6) {

                System.out.println("Please give a6-digit AMKA number");
                amkaNumber = sc.nextInt();
                flag = false;
                for(int i=0; i<100; i++) {
                    if (object3.getAmkaNumber(i) == amkaNumber) {
                        flag = true;
                        if (object3.getTeacherData(i)!=null){
                        System.out.println(object3.getTeacherData(i));
                        }
                    }
                }
                if (flag == false) {
                    System.out.println("Invalid teacher AMKA number!");

                } 
            }

            }while(choice != 7);


            sc.close();
        }

    }







0 个答案:

没有答案