저는 Java에 대한 첫 프로그래밍 할당 작업을하고 있는데 또 다른 질문이있었습니다. Student []의 내부에 Course []를 넣었지만 NullPointerException 오류가 발생하는 것 같아서 그 이유를 알 수 없습니다. 교육 과정은 교육 과정에 대한 정보를 가지고이 방법으로 "courses"의 데이터에 돌아가거나 어떻게 든 액세스 할 수 있습니까?
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
하고, 학생들은 학생에 대한 정보가 있습니다
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;
}
입력 파일의 형식이다.
이클립스를 사용하는 경우 우선 Ctrl + Shift + F를 눌러보십시오. 자동 형식이므로 좋은 formating을 사용하는 습관에 들어갈 때까지 코드를 읽기 쉽게 만듭니다. 너 스스로. – gbtimmon
'NullPointerException'이 발생하는 곳을 지정할 수 있습니까? 파일을 찾지 못하면'inputStream'이 null이기 때문에 Scanner 객체와 관련된 모든 코드가'try ... catch' 블록 안에 캡슐화되어야한다고 추측 할 수 있습니다. – Makoto