英文:
(java beginner) i do not know how to overcome the null error
问题
// 班级表达式
一所位于雅典的幼儿园开设了婴儿和幼儿班,希望创建一个应用程序来记录每个学年学生的历史。
该系统支持以下功能:
1. 打印所有已经或正在就读的学生
2. 注册一个新的学生在当前学年
3. 从当前学年中删除一个学生
4. 根据用户设置的定义(例如,班级和学年,如"2017-2018")搜索并打印课程(学生和教师)
5. 查找每位学校教师所教的课程
6. 通过AMKA查找教师数据
建模:
每个学生由以下信息描述:
唯一编号(id)
全名
出生日期
创建的对象存储在静态类型的学生表中,有100个位置可供调用。
每位教师由以下信息描述:
唯一编号(id)
全名
AMKA
创建的对象存储在静态类型的教师表中,有100个位置可供调用。
每个学年由以下信息描述:
学年,例如"2019-2020"
类型为学生表的每个位置中也存储着一名学生(学生类型变量),该学生在今年就读幼儿班(这个表不是上述提到的静态表,而是一个变量快照,描述每个学年的幼儿班)
幼儿班的教师(教师类型变量)
类型为学生表的每个位置中也存储着一名学生(学生类型变量),该学生在今年就读婴儿班(这个表是上述提到的静态表,是一个变量快照,描述每个学年的婴儿班)
婴儿班的教师(教师类型变量)
这些表的大小随着年份的变化而变化,但在年内不能改变。这意味着创建学年时,必须设置这些表的大小。不要求班级必须由学生填满,但不能在每个班级中学习的学生数超过相应表的大小。因此,我们在表中的可用位置中存储每个学生(空缺表示内容为空)。
创建的对象存储在静态类型的学年表中,有100个位置可供调用。
功能性
程序由前面描述的3个类以及一个负责启动和运行程序的类组成,该类包括主方法并打印以下选项菜单:
~~~~~~~~~~ 选项菜单 ~~~~~~~~~~
1. 打印所有已经就读学生
2. 注册一个新的学生在当前学年
3. 从当前学年中删除一个学生
4. 搜索当前学年的数据
5. 搜索每位学校教师所教的课程
6. 通过AMKA查找教师信息
7. 退出
我有一种感觉,这个错误来自于我为了创建对象3而创建的教师“空”构造函数。如果是这样,我应该如何在不使用其他构造函数参数的情况下创建Teacher类的对象?因为通过创建对象3,我不想创建一个真正的教师,而是要用它做其他用途,比如调用Teacher方法等。如果我让你感到困惑,我很抱歉,我是初学者!
谢谢你的时间!
我得到的错误是:
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
英文:
Job Expression
A kindergarten in Athens that has classes for infants and toddlers wants to create an application to keep
a history of its students per school year.
The system supports the following functions:
-
Print all students who have attended or have attended the station
-
Registration of a new student in the current school year
-
Delete a student from the current school year
-
Search and print the classes (students and teachers) based on the definitions set by the user (which
class eg infant and the year of study eg "2017-2018" -
Search for classes taken by each school teacher
-
Search for teacher data via AMKA
Modeling
Each student is described by:
Unique Code (id)
Full name
Date of birth
The objects created are stored in a static type table Student 100 seats to be available for recall.
Each teacher is described by:
Unique Code (id)
Full name
AMKA
The objects created are stored in a static type table Teacher 100 seats to be available for recall.
Each school year is described by:
The year of study, for example "2019-2020"
Type table Student in each position of which a student is also stored
(Student type variable) who is studying in the preschool class this year. (This table
is not the
static table mentioned above. This is a variable snapshot as it describes the class of preschoolers
each school year)
And The teacher of the preschool class ( Teacher type variable)
And Type table Student in each position of which a student is also stored
(Student type variable) who is studying this year in the infant class. (This table is the static
mentioned above. This is a variable snapshot as it describes the class of infants each school
year)
And The infant class teacher ( Teacher type variable)
And The size of the tables with the number of students may change from year to year, but may not
change during the year. This means that when a school year is created, the size of these tables
must be set. It is not necessary for the classes to be complete by students but in no case can a
number greater than the size of the corresponding tables be studied in each class. That's why
we store each student in available positions in the table (vacancy means that it has null content)
The objects created are stored in a static type table SchoolYear
100 seats to be available for recall.
Functionality
The program consists of the 3 classes described in the previous section and a class responsible for
starting and running the program which includes the main method and prints the following options
menu:
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();
}
}
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论