0

I get the error Exception in thread "main" java.lang.NullPointerException when i run my program and i do not know what this means. If someone could give me a hint that would be greatly appreciated.I have pasted 3 of my classes. thanks

The entire error that it gives me is

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

  }
}

`

3
  • woops i didnt copy the entire code for the course grades Commented Mar 24, 2015 at 4:37
  • you can edit the question for correction. Commented Mar 24, 2015 at 4:42
  • Your question probably already has an answer here: "What is a Null Pointer Exception, and how do I fix it?" Commented Mar 24, 2015 at 13:50

1 Answer 1

1

The exception clearly shows the line numbers

at CourseGrades.toString(CourseGrades.java:107)

In CourseGrades toString() method you haven't performed any null check.

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

In TestCourseGrades, you've set only LabValue of course instance but not others. So the values for grade[1] ,grade[2], grade[3] values will be null.

That's why you're getting null pointer exception

Modify the toString() method like the below

    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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.