gpt4 book ai didi

java - 我在程序中得到(线程 "main"java.lang.NullPointerException 中的异常)

转载 作者:行者123 更新时间:2023-12-01 11:44:57 25 4
gpt4 key购买 nike

当我运行程序时,我收到错误 Exception in thread "main"java.lang.NullPointerException ,但我不知道这意味着什么。如果有人能给我一个提示,我将不胜感激。我已经粘贴了 3 门类(class)。谢谢

它给我的整个错误是

Exception in thread "main" java.lang.NullPointerException
at CourseGrades.toString(CourseGrades.java:107)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at TestCourseGrades.main(TestCourseGrades.java:23)

	FinalExam finalExam) {

grade[0] = labMark;
grade[1] = passFailExam;
grade[2] = essay;
grade[3] = finalExam;
}

public void setLab(GradedActivity labMark) {

grade[0] = labMark;
}

public void setPassFailExam(PassFailExam exam) {


grade[1] = exam;
}

public void setEssay(Essay essay) {


grade[2] = essay;
}

public void setFinalExam(FinalExam finalexam) {

grade[3] = finalexam;
}
public double getAverage() {

double average;

double total = 0;

for (GradedActivity currentArrayValue: grade) {
total += currentArrayValue.getScore();
}

average = total / grade.length;

return average;

}


public GradedActivity getLowest() {

GradedActivity lowest = grade[0];

for (GradedActivity currentArrayValue: grade) {
if (currentArrayValue.getScore() > lowest.getScore()) {
lowest = currentArrayValue;
}
}

return lowest;




}


public GradedActivity getHighest() { // ASK IF THERE IS A WAY TO DO IT WITHOUT ENHCANCED FOR LOOP

GradedActivity highest = grade[0]; // setting highest to the first value

for (GradedActivity currentArrayValue: grade) // going through grade array and holding the value in currentArray value
{
if (currentArrayValue.getScore() > highest.getScore()) // comparing the current array value being looped with the first value that has been set
{
highest = currentArrayValue; // if the current value is bigger than initial value than we put it as the highest
}
}

return highest; // returs the highest value
}



public String toString() {
String str; // FOR LOOP???

str = "Lab Score: " + grade[0].getScore() + ", Grade: " + grade[0].getGrade() + "\n" +
"Pass/Fail Exam Score: " + grade[1].getScore() + ", Grade: " + grade[1].getGrade() +
"Essay Score: " + grade[2].getScore() + ", Grade: " + grade[2].getGrade() + "\n" +
"Final Exam Score: " + grade[3].getScore() + ", Grade: " + grade[3].getGrade();

return str;
}

}

public class GradedActivity {

private double score;
private char letterGrade;


public GradedActivity() {

}

public GradedActivity(double lb) {
score = lb;
}

public void setScore(double s) {
score = s;
}

public void setLabMark(double lb) {
score = lb;
}


public void setLetterGrade(char lg) {
letterGrade = lg;
}
public double getScore() {
return score;
}
public double getLabMark() {
return score;
}

public char getGrade() {


if (score >= 90)
letterGrade = 'A';
else if (score >= 80)
letterGrade = 'B';
else if (score >= 70)
letterGrade = 'C';
else if (score >= 60)
letterGrade = 'D';
else
letterGrade = 'F';

return letterGrade;

}


public String toString() {
String str;
str = "Score: " + score + " Grade: " + letterGrade;
return str;
}
}

import javax.swing.JOptionPane;
public class TestCourseGrades {
public static void main(String[] args) {

String input;
double labMark;
int numQuestions;
int numMissed;
double pointsEach;
int grammar;
int spelling;
int correctLength;
int content;
CourseGrades course = new CourseGrades();

//Get lab mark
input = JOptionPane.showInputDialog("What is your lab mark?");
labMark = Double.parseDouble(input);
GradedActivity lab = new GradedActivity(labMark);
System.out.println("Lab mark is: " + lab);
course.setLab(lab);
System.out.println(course);

//Get pass/fail criteria
input = JOptionPane.showInputDialog("Pass/Fail exam: How many questions?");
numQuestions = Integer.parseInt(input);
input = JOptionPane.showInputDialog("How many questions missed?");
numMissed = Integer.parseInt(input);
input = JOptionPane.showInputDialog("minium passing mark?");
pointsEach = Double.parseDouble(input);
PassFailExam passFailExam = new PassFailExam(numQuestions, numMissed, pointsEach);
System.out.println("Pass Fail Exam:\n" + passFailExam);
course.setPassFailExam(passFailExam);
System.out.println(course);


// Get Essay marks
input = JOptionPane.showInputDialog("Essay: Grammar out of 30: ");
grammar = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Essay: Spelling out of 20: ");
spelling = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Essay: Correct Length out of 20: ");
correctLength = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Essay: Content out of 30: ");
content = Integer.parseInt(input);
Essay essay = new Essay(grammar, spelling, correctLength, content);
System.out.println("Essay:\n" + essay);
course.setEssay(essay);
System.out.println(course);

//Get final exam
input = JOptionPane.showInputDialog("Final exam: How many questions?");
numQuestions = Integer.parseInt(input);
input = JOptionPane.showInputDialog("How many questions missed?");
numMissed = Integer.parseInt(input);
FinalExam finalExam = new FinalExam(numQuestions, numMissed);
System.out.println(finalExam);
course.setFinalExam(finalExam);
System.out.println(course);
//Create a course grade
//Set up the CourseGrades
CourseGrades courseCS = new CourseGrades(lab, passFailExam, essay, finalExam);
System.out.println("Test another course:\n" + courseCS);
System.out.println("The average of all exams is: " + courseCS.getAverage());
GradedActivity highest = courseCS.getHighest();
GradedActivity lowest = courseCS.getLowest();
System.out.println("The highest mark is: " + highest.getScore());
System.out.println("The lowest mark is: " + lowest.getScore());

}
}

`

最佳答案

异常清楚地显示了行号

at CourseGrades.toString(CourseGrades.java:107)

在 CourseGrades toString() 方法中,您尚未执行任何 null 检查。

 str = "Lab Score: " + grade[0].getScore() + ", Grade: " + grade[0].getGrade() + "\n" +
"Pass/Fail Exam Score: " + grade[1].getScore() + ", Grade: " + grade[1].getGrade() +
"Essay Score: " + grade[2].getScore() + ", Grade: " + grade[2].getGrade() + "\n" +
"Final Exam Score: " + grade[3].getScore() + ", Grade: " + grade[3].getGrade();

在 TestCourseGrades 中,您仅设置了类(class)实例的 LabValue,但未设置其他值。因此,grade[1]、grade[2]、grade[3] 的值将为 null。

这就是为什么你会遇到空指针异常

修改 toString() 方法,如下所示

    public String toString() {
String str=""; // FOR LOOP???

if(grade[0]!=null)
str += "Lab Score: " + grade[0].getScore() + ", Grade: " + grade[0].getGrade() + "\n";
if(grade[1]!=null)
str += "Pass/Fail Exam Score: " + grade[1].getScore() + ", Grade: " + grade[1].getGrade() ;
if(grade[2]!=null)
str += "Essay Score: " + grade[2].getScore() + ", Grade: " + grade[2].getGrade() + "\n" ;
if(grade[3]!=null)
str += "Final Exam Score: " + grade[3].getScore() + ", Grade: " + grade[3].getGrade();

return str;
}

关于java - 我在程序中得到(线程 "main"java.lang.NullPointerException 中的异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29225087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com