!-链接到输入文件https://drive.google.com/open?id=1b1vH0wpmonZGlWxQsLa4TJpE2gFyqIOm->
我一直收到NullPointerException错误,并且已经花了几个小时才找到修复程序。
这是错误----
Exception in thread "main" java.lang.NullPointerException
at fscgradebookv2.FSCgradebookV2.displayStats(FSCgradebookV2.java:96)
at fscgradebookv2.FSCgradebookV2.main(FSCgradebookV2.java:223)
这是我的项目代码。第二段代码是一个学生班。
!-----这是FSCgradebookV2.java ---------!
package fscgradebookv2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.naming.ldap.LdapName;
import javax.xml.transform.OutputKeys;
public class FSCgradebookV2 {
public static FSCstudent addRecord(int id, String fName, String lName, int g1, int g2, int g3) {
int index;
int[] grades = {g1, g2, g3};
double finalGrade = g1 * (0.3) + g2 * (0.3) + g3 * (0.4);
char letterGrade = getLetterGrade(finalGrade);
// if (finalGrade >= 90) {
// letterGrade = 'A';
// } else if (finalGrade >= 80) {
// letterGrade = 'B';
// } else if (finalGrade >= 70) {
// letterGrade = 'C';
// } else if (finalGrade >= 60) {
// letterGrade = 'D';
// } else {
// letterGrade = 'F';
// }
FSCstudent temp = new FSCstudent(id, fName, lName, grades, finalGrade, letterGrade);
FSCstudent.increaseStudents(1);
return temp;
}
public static void searchByID(int idSearch, int maxNum, FSCstudent[] students, PrintWriter output) {
//File outputFile = new File("FSCgradebookV2.out");
//PrintWriter output = new PrintWriter(outputFile);
int numStudents = FSCstudent.getNumStudents();
// I created two outputs that go to the same file
if (numStudents > 0) { // nvm haha
for (int i = 0; i < numStudents; i++) { //
if (students[i].getId() == idSearch) { //Make so if no id matches you get an error explaining
output.printf(" - Student Record for %s %s (ID # %d): \n", students[i].getfName(),
students[i].getlName(), students[i].getId());
output.printf(" Exam 1: %d\n", students[i].getExamGrades()[0]); //
output.printf(" Exam 2: %d\n", students[i].getExamGrades()[1]);
output.printf(" Final Exam: %d\n", students[i].getExamGrades()[2]);
output.printf(" Final Grade: %.2f\n", students[i].getFinalGrade());
output.printf(" Letter Grade: %c\n\n", students[i].getLetterGrade());
}
}
} else {
output.println("ERROR: cannot perform search. The gradebook is empty (no students added yet).");
}
}
public static void searchByName(String fnameSearch, String lnameSearch, FSCstudent[] students, PrintWriter output) {
int numStudents = FSCstudent.getNumStudents();
if (numStudents > 0) {
for (int i = 0; i < numStudents; i++) { //
if (students[i].getfName().equals(fnameSearch)) { //Make so if no id matches you get an error explaining
if (students[i].getlName().equals(lnameSearch)) {
output.printf(" - Student Record for %s %s (ID # %d): \n", students[i].getfName(),
students[i].getlName(), students[i].getId());
output.printf(" Exam 1: %d\n", students[i].getExamGrades()[0]); //
output.printf(" Exam 2: %d\n", students[i].getExamGrades()[1]);
output.printf(" Final Exam: %d\n", students[i].getExamGrades()[2]);
output.printf(" Final Grade: %.2f\n", students[i].getFinalGrade());
output.printf(" Letter Grade: %c\n\n", students[i].getLetterGrade());
}
}
}
} else {
output.println("ERROR: cannot perform search. The gradebook is empty (no students added yet).");
}
}
public static void displayStats(String className, String teachName, PrintWriter output, FSCstudent[] students) {
int numStudents = FSCstudent.getNumStudents();
output.printf("Statistical Results of %s (Instructor: %s): \n", className, teachName);
output.printf(" Total number of student records: %d", numStudents);
int numA, numB, numC, numD, numF;
numA = 0;
numB = 0;
numC = 0;
numD = 0; //Aight bet htnkas
numF = 0;
double highest_grade = 0.0;
double lowest_grade = 100.0;
int grades_sum = 0;
for (int i = 0; i < numStudents; i++) {
grades_sum += students[i].getFinalGrade();
if (students[i].getFinalGrade() < lowest_grade) {
lowest_grade = students[i].getFinalGrade();
}
if (students[i].getFinalGrade() > highest_grade) {
highest_grade = students[i].getFinalGrade();
}
if (students[i].getLetterGrade() == 'A') {
numA++;
} else if (students[i].getLetterGrade() == 'B') {
numB++;
} else if (students[i].getLetterGrade() == 'C') {
numC++;
} else if (students[i].getLetterGrade() == 'D') {
numD++;
} else if (students[i].getLetterGrade() == 'F') {
numF++;
}
}
double average_grade = grades_sum / FSCstudent.getNumStudents();
if (FSCstudent.getNumStudents() == 0) {
lowest_grade = 0.0;
}
output.printf(" Average Score: %.2f\n", average_grade);
output.printf(" Highest Score: %.2f\n", highest_grade);
output.printf(" Lowest Score: %.2f\n", lowest_grade);
output.printf(" Total 'A' Grades: %d (%.2f %% of class)", numA, (double) numA / FSCstudent.getNumStudents());
output.printf(" Total 'B' Grades: %d (%.2f %% of class)", numB, (double) numB / FSCstudent.getNumStudents());
output.printf(" Total 'C' Grades: %d (%.2f %% of class)", numC, (double) numC / FSCstudent.getNumStudents());
output.printf(" Total 'D' Grades: %d (%.2f %% of class)", numD, (double) numD / FSCstudent.getNumStudents());
output.printf(" Total 'F' Grades: %d (%.2f %% of class)", numF, (double) numF / FSCstudent.getNumStudents());
}
public static void displayStudents() {
}
public static char getLetterGrade(double finalGrade) {
char letterGrade;
if (finalGrade >= 90) {
letterGrade = 'A';
} else if (finalGrade >= 80) {
letterGrade = 'B';
} else if (finalGrade >= 70) {
letterGrade = 'C';
} else if (finalGrade >= 60) {
letterGrade = 'D';
} else {
letterGrade = 'F';
}
return letterGrade;
}
public static void main(String[] args) throws FileNotFoundException {
// Create the command and open the input file. Also create the output file
String command;
File inputFile = new File("FSCgradebookV2.in");
if (!inputFile.exists()) {
System.out.println("Input file, " + inputFile + ", does not exist.");
System.exit(0);
}
File outputFile = new File("FSCgradebookV2.out");
Scanner in = new Scanner(inputFile);
PrintWriter output = new PrintWriter(outputFile);
String className = in.nextLine();
String teachName = in.nextLine();
int maxNum = Integer.parseInt(in.nextLine());
FSCstudent[] students = new FSCstudent[maxNum];
int nextStudentIndex = 0;
// Using a do while here because Dr. Cazalas did. HEY JAVA
do {
command = in.next();
if (command.equals("ADDRECORD") == true) {
output.println("Command: ADDRECORD");
int id = Integer.parseInt(in.next());
String fName = in.next();
String lName = in.next();
int g1 = Integer.parseInt(in.next());
int g2 = Integer.parseInt(in.next());
int g3 = Integer.parseInt(in.next());
FSCstudent student = addRecord(id, fName, lName, g1, g2, g3);
int i = nextStudentIndex - 1;
for (; i >= 0; i--) {
if (students[i].getId() > student.getId()) {
students[i + 1] = students[i];
} else {
break;
}
}
students[i + 1] = student;
output.printf("%s %s (ID# %d) has been added to the FSC Grade Book.", fName, lName, id);
output.println();
output.printf(" Final Grade: %.2f (%c).\n\n", student.getFinalGrade(), student.getLetterGrade());
} else if (command.equals("SEARCHBYID") == true) {
output.println("Command: SEARCHBYID");
int idSearch = Integer.parseInt(in.next());
searchByID(idSearch, maxNum, students, output); //It should still print the addrecord stuff. That was working earlier
//searchByID(idSearch, maxNum , FSCstudent[] students);
} else if (command.equals("SEARCHBYNAME") == true) {
output.println("Command: SEARCHBYNAME");
String fnameSearch = in.next();
String lnameSearch = in.next();
searchByName(fnameSearch, lnameSearch, students, output);
} else if (command.equals("DISPLAYSTATS") == true) {
if (FSCstudent.getNumStudents() == 0) {
output.printf("Statistical Results of %s (Instructor: %s): \n", className, teachName);
output.printf(" Total number of student records: %d", FSCstudent.getNumStudents());
output.println("Total number of student records: 0");
output.println("Average Score: 0.00");
output.println(" Score: 0.00");
output.println("Lowest Score: 0.00");
output.println("Total 'A' Grades: 0 (0% of class)");
output.println("Total 'B' Grades: 0 (0% of class)");
output.println("Total 'C' Grades: 0 (0% of class)");
output.println("Total 'D' Grades: 0 (0% of class)");
output.println("Total 'F' Grades: 0 (0% of class)");
output.println();
}
else{
output.println("Command: DISPLAYSTATS");
displayStats(className, teachName, output, students);
}
} else if (command.equals("DISPLAYSTUDENTS") == true) {
output.println("Command: DISPLAYSTUDENTS");
//
} // Command QUIT: Quit the Program
else if (command.equals("QUIT") == true) {
output.println("Thank you for using the FSC Grade Book.");
output.println("Goodbye.");
} // Invalid Command
else {
System.out.println("Invalid Command: input invalid.");
}
} while (command.equals("QUIT") != true);
// Close input and output
in.close();
output.close();
}
}
这是我的第二段代码!---- FSCstudent.java ---!
package fscgradebookv2;
public class FSCstudent {
private String fName;
private String lName;
private int id;
private int examGrades[];
private double finalGrade;
private char letterGrade;
//private int numStudents = 0;
private static int numStudents = 0;
public FSCstudent(){
}
public FSCstudent(int id, String fName, String lName, int examGrades[], double finalGrade , char letterGrade){
this.id = id;
this.fName = fName;
this.lName = lName;
this.examGrades = examGrades;//
this.finalGrade = finalGrade;
this.letterGrade = letterGrade;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int[] getExamGrades() {
return examGrades; //
}
public void setExamGrades(int[] examGrades) {
this.examGrades = examGrades;
}
public double getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(double finalGrade) {
this.finalGrade = finalGrade;
}
public char getLetterGrade() {
return letterGrade;
}
public void setLetterGrade(char letterGrade) {
this.letterGrade = letterGrade;
}
public static int getNumStudents() {
return numStudents;
}
public static void increaseStudents(int numStudents) {
FSCstudent.numStudents += numStudents;
}
}
这是从.in文件名FSCgradebookV2.in获取命令
以下是命令:
CSC 2290
Jonathan Cazalas
200
DISPLAYSTUDENTS
DISPLAYSTATS
SEARCHBYID 111
SEARCHBYNAME Joe Smith
ADDRECORD 111 Joe Smith 90 85 94
SEARCHBYNAME Joe Smith
SEARCHBYID 111
DISPLAYSTATS
DISPLAYSTUDENTS
ADDRECORD 871 Jason Borne 90 70 82
ADDRECORD 253 Clark Kent 40 80 60
DISPLAYSTATS
SEARCHBYID 871
SEARCHBYNAME Clark Kent
DISPLAYSTUDENTS
ADDRECORD 584 Student PlaysALot 30 20 28
DISPLAYSTUDENTS
DISPLAYSTATS
ADDRECORD 777 Student WhoWorksHard 100 100 100
DISPLAYSTATS
DISPLAYSTUDENTS
SEARCHBYNAME Student WhoWorksHard
SEARCHBYID 111
SEARCHBYID 777
SEARCHBYID 871
SEARCHBYID 222
SEARCHBYNAME Donald Duck
QUIT
和预期的输出
Welcome to the FSC Grade Book.
Command: DISPLAYSTUDENTS
ERROR: there are no students currently in the system.
Command: DISPLAYSTATS
Statistical Results of CSC 2290 (Instructor: Dr Jonathan Cazalas):
Total number of student records: 0
Average Score: 0.00
Highest Score: 0.00
Lowest Score: 0.00
Total 'A' Grades: 0 (0% of class)
Total 'B' Grades: 0 (0% of class)
Total 'C' Grades: 0 (0% of class)
Total 'D' Grades: 0 (0% of class)
Total 'F' Grades: 0 (0% of class)
Command: SEARCHBYID
ERROR: cannot perform search. The gradebook is empty (no students added yet).
Command: SEARCHBYNAME
ERROR: cannot perform search. The gradebook is empty (no students added yet).
Command: ADDRECORD
Joe Smith (ID# 111) has been added to the FSC Grade Book.
Final Grade: 90.10 (A).
Command: SEARCHBYNAME
- Student Record for Joe Smith (ID # 111):
Exam 1: 90
Exam 2: 85
Final Exam: 94
Final Grade: 90.10
Letter Grade: A
Command: SEARCHBYID
- Student Record for Joe Smith (ID # 111):
Exam 1: 90
Exam 2: 85
Final Exam: 94
Final Grade: 90.10
Letter Grade: A
Command: DISPLAYSTATS
Statistical Results of CSC 2290 (Instructor: Dr Jonathan Cazalas):
Total number of student records: 1
Average Score: 90.10
Highest Score: 90.10
Lowest Score: 90.10
Total 'A' Grades: 1 (100.00% of class)
Total 'B' Grades: 0 ( 0.00% of class)
Total 'C' Grades: 0 ( 0.00% of class)
Total 'D' Grades: 0 ( 0.00% of class)
Total 'F' Grades: 0 ( 0.00% of class)
Command: DISPLAYSTUDENTS
***Class Roster and Grade Sheet***
- Student Record for Joe Smith (ID # 111):
Exam 1: 90
Exam 2: 85
Final Exam: 94
Final Grade: 90.10
Letter Grade: A
Command: ADDRECORD
Jason Borne (ID# 871) has been added to the FSC Grade Book.
Final Grade: 80.80 (B).
Command: ADDRECORD
Clark Kent (ID# 253) has been added to the FSC Grade Book.
Final Grade: 60.00 (D).
Command: DISPLAYSTATS
Statistical Results of CSC 2290 (Instructor: Dr Jonathan Cazalas):
Total number of student records: 3
Average Score: 76.97
Highest Score: 90.10
Lowest Score: 60.00
Total 'A' Grades: 1 ( 33.33% of class)
Total 'B' Grades: 1 ( 33.33% of class)
Total 'C' Grades: 0 ( 0.00% of class)
Total 'D' Grades: 1 ( 33.33% of class)
Total 'F' Grades: 0 ( 0.00% of class)
Command: SEARCHBYID
- Student Record for Jason Borne (ID # 871):
Exam 1: 90
Exam 2: 70
Final Exam: 82
Final Grade: 80.80
Letter Grade: B
Command: SEARCHBYNAME
- Student Record for Clark Kent (ID # 253):
Exam 1: 40
Exam 2: 80
Final Exam: 60
Final Grade: 60.00
Letter Grade: D
Command: DISPLAYSTUDENTS
***Class Roster and Grade Sheet***
- Student Record for Joe Smith (ID # 111):
Exam 1: 90
Exam 2: 85
Final Exam: 94
Final Grade: 90.10
Letter Grade: A
- Student Record for Clark Kent (ID # 253):
Exam 1: 40
Exam 2: 80
Final Exam: 60
Final Grade: 60.00
Letter Grade: D
- Student Record for Jason Borne (ID # 871):
Exam 1: 90
Exam 2: 70
Final Exam: 82
Final Grade: 80.80
Letter Grade: B
Command: ADDRECORD
Student PlaysALot (ID# 584) has been added to the FSC Grade Book.
Final Grade: 26.20 (F).
Command: DISPLAYSTUDENTS
***Class Roster and Grade Sheet***
- Student Record for Joe Smith (ID # 111):
Exam 1: 90
Exam 2: 85
Final Exam: 94
Final Grade: 90.10
Letter Grade: A
- Student Record for Clark Kent (ID # 253):
Exam 1: 40
Exam 2: 80
Final Exam: 60
Final Grade: 60.00
Letter Grade: D
- Student Record for Student PlaysALot (ID # 584):
Exam 1: 30
Exam 2: 20
Final Exam: 28
Final Grade: 26.20
Letter Grade: F
- Student Record for Jason Borne (ID # 871):
Exam 1: 90
Exam 2: 70
Final Exam: 82
Final Grade: 80.80
Letter Grade: B
Command: DISPLAYSTATS
Statistical Results of CSC 2290 (Instructor: Dr Jonathan Cazalas):
Total number of student records: 4
Average Score: 64.28
Highest Score: 90.10
Lowest Score: 26.20
Total 'A' Grades: 1 ( 25.00% of class)
Total 'B' Grades: 1 ( 25.00% of class)
Total 'C' Grades: 0 ( 0.00% of class)
Total 'D' Grades: 1 ( 25.00% of class)
Total 'F' Grades: 1 ( 25.00% of class)
Command: ADDRECORD
Student WhoWorksHard (ID# 777) has been added to the FSC Grade Book.
Final Grade: 100.00 (A).
Command: DISPLAYSTATS
Statistical Results of CSC 2290 (Instructor: Dr Jonathan Cazalas):
Total number of student records: 5
Average Score: 71.42
Highest Score: 100.00
Lowest Score: 26.20
Total 'A' Grades: 2 ( 40.00% of class)
Total 'B' Grades: 1 ( 20.00% of class)
Total 'C' Grades: 0 ( 0.00% of class)
Total 'D' Grades: 1 ( 20.00% of class)
Total 'F' Grades: 1 ( 20.00% of class)
Command: DISPLAYSTUDENTS
***Class Roster and Grade Sheet***
- Student Record for Joe Smith (ID # 111):
Exam 1: 90
Exam 2: 85
Final Exam: 94
Final Grade: 90.10
Letter Grade: A
- Student Record for Clark Kent (ID # 253):
Exam 1: 40
Exam 2: 80
Final Exam: 60
Final Grade: 60.00
Letter Grade: D
- Student Record for Student PlaysALot (ID # 584):
Exam 1: 30
Exam 2: 20
Final Exam: 28
Final Grade: 26.20
Letter Grade: F
- Student Record for Student WhoWorksHard (ID # 777):
Exam 1: 100
Exam 2: 100
Final Exam: 100
Final Grade: 100.00
Letter Grade: A
- Student Record for Jason Borne (ID # 871):
Exam 1: 90
Exam 2: 70
Final Exam: 82
Final Grade: 80.80
Letter Grade: B
Command: SEARCHBYNAME
- Student Record for Student WhoWorksHard (ID # 777):
Exam 1: 100
Exam 2: 100
Final Exam: 100
Final Grade: 100.00
Letter Grade: A
Command: SEARCHBYID
- Student Record for Joe Smith (ID # 111):
Exam 1: 90
Exam 2: 85
Final Exam: 94
Final Grade: 90.10
Letter Grade: A
Command: SEARCHBYID
- Student Record for Student WhoWorksHard (ID # 777):
Exam 1: 100
Exam 2: 100
Final Exam: 100
Final Grade: 100.00
Letter Grade: A
Command: SEARCHBYID
- Student Record for Jason Borne (ID # 871):
Exam 1: 90
Exam 2: 70
Final Exam: 82
Final Grade: 80.80
Letter Grade: B
Command: SEARCHBYID
ERROR: there is no record for student ID # 222.
Command: SEARCHBYNAME
ERROR: there is no record for student "Donald Duck".
Thank you for using the FSC Grade Book.
Goodbye.
最佳答案
问题在于,当初始化时数组仅包含null时,您仅尝试访问student []对象数组中的数据(尚无Student对象)。
下列方法需要此行来捕获这种情况:
if (students[i] == null) { return; }
在下面列出的每个方法中,为第一个for循环添加为第一个代码块行,例如:
FSCgradebookV2.displayStats()
for (int i = 0; i < numStudents; i++) {
if (students[i] == null) { return; } // <-- HERE
grades_sum += students[i].getFinalGrade();
// ... other code for this 'for' loop ...
}
FSCgradebookV2.searchByID()
for (int i = 0; i < numStudents; i++) {
if (students[i] == null) { return; }
if (students[i].getId() == idSearch) {
// ... The remaining code for this 'for' loop ...
}
FSCgradebookV2.searchByName()
for (int i = 0; i < numStudents; i++) {
if (students[i] == null) { return; }
if (students[i].getfName().equals(fnameSearch)) {
// ... The remaining code for this 'for' loop ...
}
关于java - 我一直收到NullPointerException错误,并且已经花了几个小时才找到修复程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60982826/