当前,我正在做一个处理ArrayList类的任务。在某个时候,我需要检查讲师的ID,并确保没有将讲师两次添加到ArrayList中,所以我进行了一个for循环以遍历所有已注册的ID并获取该ID并进行检查如果已经存在
问题是,当我在循环中使用方法“ .size()”时,JVM抛出NullPointerException而且我不知道为什么。
================================================ =========================
我需要读的是这个:
\\名称-ID-dateOfBirth-性别-学位-专业-城市-可用性
[Amanda Smith,102020,320101200000,M,PhD,Software Engineering,NewYork,true
================================================ =======================
这是代码:
public static void main(String[] args) {
/* NOTE: I HAVE A CLASS CALLED "UniversityMember" THAT IS A SUPERCLASS FOR "Instructor" CLASS */
//declare what I need
ArrayList<UniversityMember> membersList;
Scanner read = new Scanner("inputFile.txt");//the file contains the text above
//First: Split the line everytime the sign ", " shows
String[] line = read.nextLine().split(", ");
//Second: Assign each valuse to its correspondeding variable
String name = line[0];
String id = line[1];
long date = Long.parseLong(line[2]);
Date birthDate = new Date(date);
char gender = line[3].charAt(0);
String degree = line[4];
String specialization = line[5];
String address = line[6];
boolean availability = Boolean.parseBoolean(line[7]);
//check if the Id is registered already
for (int i = 0; i < membersList.size(); i++) { //ERROR OCCURE
if (membersList.get(i) == null) {
break;
}
if (membersList.get(i).id.equals(id)) {
System.out.println("The instructor is registered already, the ID is found in the system.");
System.exit(0);
}
}
//add and make a new object for the constructor
membersList.add(new Instructor(name, id, birthDate, gender, degree, specialization, address, availability));
System.out.println("The instructor is successfully added.");
}//end main
问题是,当您在其上调用.size()时,membersList不存在
而不是
ArrayList<UniversityMember> membersList;
您需要初始化它
ArrayList<UniversityMember> membersList = new ArrayList<UniversityMember>();
您需要初始化ArrayList。像那样ArrayList MembersList = new ArrayList();之后,在第一个size()中返回0而不是null。请记住,所有数据结构必须在java中初始化。
您尚未将任何内容添加到membersList,然后要求其中不包含任何内容的大小。发生什么事的例子
String str;
for(int i = 0; i < str.length(); i++){
System.out.println("hey");
}
还需要像这样声明数组列表
ArrayList<Method name> membersList = new ArrayList<Method name>();
也不要忘记导入ArrayList类
import java.util.ArrayList;
nvm,我发现我还没有初始化我的数组(╥ω╥)我将保留此问题,以供其他人谨慎
================================================= ==
修复后的代码:
public static void main(String[] args) {
/* NOTE: I HAVE A CLASS CALLED "UniversityMember" THAT IS A SUPERCLASS FOR "Instructor" CLASS */
//declare what I need
ArrayList<UniversityMember> membersList;
Scanner read = new Scanner("inputFile.txt");//the file contains the text above
/* ===== FIXING THE ERROR ======*/
membersList = new ArrayList();
//First: Split the line everytime the sign ", " shows
String[] line = read.nextLine().split(", ");
//Second: Assign each valuse to its correspondeding variable
String name = line[0];
String id = line[1];
long date = Long.parseLong(line[2]);
Date birthDate = new Date(date);
char gender = line[3].charAt(0);
String degree = line[4];
String specialization = line[5];
String address = line[6];
boolean availability = Boolean.parseBoolean(line[7]);
//check if the Id is registered already
for (int i = 0; i < membersList.size(); i++) {
if (membersList.get(i) == null) {
break;
}
if (membersList.get(i).id.equals(id)) {
System.out.println("The instructor is registered already, the ID is found in the system.");
System.exit(0);
}
}
//add and make a new object for the constructor
membersList.add(new Instructor(name, id, birthDate, gender, degree, specialization, address, availability));
System.out.println("The instructor is successfully added.");
}//end main