- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
!-- 链接到输入文件 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.
最佳答案
问题是,当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/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!