(Java初学者)我不知道如何解决空指针错误。

huangapple 未分类评论48阅读模式
英文:

(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:

  1. Print all students who have attended or have attended the station

  2. Registration of a new student in the current school year

  3. Delete a student from the current school year

  4. 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"

  5. Search for classes taken by each school teacher

  6. 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 &quot;empty&quot; constructor i made in order to create object3. If that&#39;s so how am i supposed to create an object of the class Teacher without using the parameters of the other constructor i&#39;ve made? Since by creating an object3, i don&#39;t want to create an actual teacher, but to use it for other purposes such as calling Teacher methods etc. I&#39;m sorry if i frustrated you but I am a beginner!!
Thank you for your time!

the error i get is:

        Exception in thread &quot;main&quot; java.lang.ExceptionInInitializerError
        	at Ergasia.YearBook.main(YearBook.java:20)
        Caused by: java.lang.NullPointerException
        	at Ergasia.Teacher.&lt;init&gt;(Teacher.java:19)
        	at Ergasia.Teacher.&lt;clinit&gt;(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(&quot;George Mill&quot;, 565637);
    	static Teacher teacherNipio = new Teacher(&quot;John Snow&quot;, 564319);
    	static Teacher teacherPronipio2019_2020 = new Teacher(&quot;Jacob Jonson&quot;, 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 + &quot; &quot; + teacherName + &quot; &quot; + 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&lt;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(&quot;Menu:&quot;);
    		System.out.println(&quot;1. Show all the Students&quot;);
    		System.out.println(&quot;2. Register a new Student in current School year&quot;);
    		System.out.println(&quot;3. Delete a Student from current School year&quot;);
    		System.out.println(&quot;4. Search Data for current School year&quot;);
    		System.out.println(&quot;5. Search every teacher&#39;s class&quot;);
    		System.out.println(&quot;6. Search teacher by AMKA number&quot;);
    		System.out.println(&quot;7.Exit&quot;);
    		
    		do {
    			System.out.print(&quot;Please pick one of the choices (1-6): &quot;);
    			choice = sc.nextInt();
    			if (choice &gt; 6 || choice &lt; 1) {
    				System.out.println(&quot;The number you gave is invalid.&quot;);
    			}
    		}while (choice &gt; 7 || choice &lt; 1);
    		 
    		if (choice == 1) {
    			int i = 0;
    			while (i &lt;= 100) {
    				if (studentData[i] != null){ 
    				System.out.println(studentData[i].toString());
    				i++;
    				}
    			}
    			
    		}else if (choice == 2) {
    			System.out.println(&quot;Would you like to Register the student\nat pronipio or Nipio? &quot;);
    			System.out.print(&quot;Press 1 for Pronipio and 2 for Nipio: &quot;);
    			choice2 = sc.nextInt();
    			while (choice2 != 1 &amp;&amp; choice2 != 2) {
    				System.out.println(&quot;The number you gave is invalid.&quot;);
    				choice2 = sc.nextInt();
    			} 
    			
    			if (choice2 == 1) {
    				int i=0;
    					while ( i &lt; 100 &amp;&amp; flag == false) {
    						if (pronipio2019_2020[i] == null) {
    							flag = true;
    							System.out.println(&quot;Please Give Name and Year of Birth: &quot;);
    							studentName = sc.nextLine();
    							yearOfBirth = sc.nextInt();
    							Student newStudent = new Student(studentName,yearOfBirth);
    							pronipio2019_2020[i] = newStudent;
    							i++;
    							int j=0;
    							while(j &lt; 100 &amp;&amp; flag == false) {
    								if (studentData[j] == null ) {
    									studentData[i] = newStudent;
    									j++;
    								}
    							}
    						}
    					}	
    			}else {
    				int i=0;
    				while ( i &lt; 100 &amp;&amp; flag == false) {
    					if (nipio2019_2020[i] == null) {
    						flag = true;
    						System.out.println(&quot;Please Give 6-digit student Id,\nName and Year of Birth: &quot;);
    						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 &lt; 100 &amp;&amp; flag == false) {
    							if (studentData[j] == null ) {
    								studentData[i] = newStudent;
    								j++;
    							}
    						}
    					}
    				}
    				
    			}
    		}else if(choice == 3) {
    			System.out.println(&quot;Please Give the 6-digit Id of the Student You Would Like to Delete: &quot;);
    			studentId = sc.nextInt();
    			int i =0;
    			flag = false;
    			while (i &lt; 100 &amp;&amp; flag == false) {
    				if (object2.getStudentId(pronipio2019_2020[i]) == studentId) {
    					flag = true;
    					System.out.print(&quot;Are you sure you want to delete this student? Yes or No: &quot;);
    						answer = sc.nextLine();
    						if(answer == &quot;yes&quot; || answer == &quot;Yes&quot;) {
    							pronipio2019_2020[i] = null;
    						}else if(answer == &quot;no&quot; || answer == &quot;No&quot;){
    							System.out.println(&quot;OK&quot;);
    						}
    				}
    			}
    			if (flag == false){
    				while (i &lt; 100 &amp;&amp; flag == false) {
    					if (object2.getStudentId(nipio2019_2020[i]) == studentId) {
    						flag = true;
    						System.out.print(&quot;Are you sure you want to delete this student? Yes or No: &quot;);
    							answer = sc.nextLine();
    							if(answer == &quot;yes&quot; || answer == &quot;Yes&quot;) {
    								nipio2019_2020[i] = null;
    							}else if(answer == &quot;no&quot; || answer == &quot;No&quot;){
    								System.out.println(&quot;OK&quot;);
    							}
    					}
    				}
    			}
    			
    		}else if(choice == 4) {
    			flag = false;
    			do {
    			System.out.print(&quot;Please Pick a School Year (2018-2019 or 2019-2020): &quot;);
    			answer = sc.nextLine();
    			if (answer == &quot;2018-2019&quot;) {
    				flag = true;
    				System.out.print(&quot;Please Pick 1 for pronipio and 2 for nipio: &quot;);
    				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 == &quot;2019-2020&quot;) {
    				flag = true;
    				System.out.print(&quot;Please Pick 1 for pronipio and 2 for nipio: &quot;);
    				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(&quot;There are no data for the year you searched for&quot;);
    			
    			}while (flag == false);
    		
    		}else if(choice == 5) {
    			for(int i = 0 ; i &lt;= 2 ; i++) {
    				for(int j = 0 ; j &lt; 4  ; j++) {
    					if(j==0 || j==1) {
    						int c1 = 0;
    						for(int c2=0 ; c2 &lt;= 1 ; c2++ ) {
    							if(teacherData[i] == schoolYearObject.getTeacher(years[c1], c2) &amp;&amp; teacherData[i]!=null) {
    								System.out.print(teacherData[i] + &quot; 2018-2019 &quot;);
    								if (i==0) {
    									System.out.println(&quot;Pronipio&quot;);
    								}else if(i==1) {
    									System.out.println(&quot;Nipio&quot;);
    								}
    							}
    						}
    						
    					}else if(j==2 || j==3) {
    						int c1 = 0;
    						for(int c2=2 ; c2 &gt;= 1 ; c2-- ) {
    							if(teacherData[i] == schoolYearObject.getTeacher(years[c1], c2) &amp;&amp; teacherData[i] != null) {
    								System.out.print(teacherData[i] + &quot; 2019-2020 &quot;);
    								if (i==2) {
    									System.out.println(&quot;Pronipio&quot;);
    								}else if(i==1) {
    									System.out.println(&quot;Nipio&quot;);
    								}
    							}
    						}
    					
    					}
    				}
    			}
    			
    		}else if(choice == 6) {
    			
    			System.out.println(&quot;Please give a6-digit AMKA number&quot;);
    			amkaNumber = sc.nextInt();
    			flag = false;
    			for(int i=0; i&lt;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(&quot;Invalid teacher AMKA number!&quot;);
    				
    			} 
    		}
    		
    		}while(choice != 7);
    			
    			
    		sc.close();
    	}
    
    }








</details>


huangapple
  • 本文由 发表于 2020年5月4日 00:54:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/61578307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定