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

时间:2015-03-24 04:35:03

标签: java nullpointerexception

我在线程" main"中得到错误Exception。 java.lang.NullPointerException当我运行我的程序时,我不知道这意味着什么。如果有人能给我一个非常感激的提示。我已经粘贴了3个班级。感谢

它给我的整个错误是

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());

  }
}




`

1 个答案:

答案 0 :(得分:0)

该例外清楚地显示了行号

at CourseGrades.toString(CourseGrades.java:107)

在CourseGrades toString()方法中,您还没有执行任何空检查。

 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中,您只设置了课程实例的LabValue而不设置其他实例。 因此,等级[1],等级[2],等级[3]值的值将为空。

这就是你获得空指针异常的原因

修改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;
}