我正在处理我的第一个 Java 编程任务,我还有另一个问题。我在 Student[] 中放了一个 Course[] 但现在似乎遇到了 NullPointerException 错误,我不知道为什么。
public Student[] analyzeData() {
Scanner inputStream = null;
try {
inputStream = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt"));
} catch (FileNotFoundException e) {
System.out.println("File Programming Assignment 1 Data.txt could not be found or opened.");
System.exit(0);
}
int numberOfStudents = inputStream.nextInt();
int tuitionPerHour = inputStream.nextInt();
Student[] students = new Student[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
String firstName = inputStream.next();
String lastName = inputStream.next();
int studentID = inputStream.nextInt();
String isTuitionPaid = inputStream.next();
int numberOfCourses = inputStream.nextInt();
Course[] courses = new Course[numberOfCourses];
for (i = 0; i < numberOfCourses; i++) {
String courseName = inputStream.next();
String courseNumber = inputStream.next();
int creditHours = inputStream.nextInt();
String grade = inputStream.next();
Course currentCourse = new Course(courseName, courseNumber, creditHours, grade);
courses[i] = currentCourse;
}
Student currentStudent = new Student(firstName, lastName, studentID, isTuitionPaid, numberOfCourses, courses);
students[i] = currentStudent;
}
return students;
}
输入文件的格式为:
3 345
Lisa Miller 890238 Y 2
Mathematics MTH345 4 A
Physics PHY357 3 B
Bill Wilton 798324 N 2
English ENG378 3 B
Philosophy PHL534 3 A
其中课程有关于课程的信息,学生有关于学生的信息。