StudentTest.java
: Has my Main()
, tests the classes I've set up.
public class StudentTest
{
public static void main(String args[])
{
Student s1 = new Student("Matt", "Patitz");
s1.setMidterm(78, "B");
Only the first couple lines because the error is on that last line.
Student.java
: The Student class, includes the setMidterm(int, String)
method I'm trying to call
import java.util.Scanner;
public class Student {
Scanner input = new Scanner(System.in);
private String FirstName;
private String LastName;
private Exam Midterm;
private Exam FinalExam;
private Coursework Homework;
private Coursework InClass;
public Student(String firstName, String lastName){
FirstName = firstName;
LastName = lastName;
}
public void setMidterm(int score, String grade){
Midterm.setScore(score);
Midterm.setLetterGrade(grade);
}
Again, not the whole class file. The error is on Midterm.setScore(score);
Exam.java
: The Exam class, includes the setScore(int)
method.
public class Exam {
private int Score;
private String LetterGrade;
public Exam(int score, String letterGrade){
if(score>100){
Score=100;
} else if(score<0){
Score=0;
} else {
Score=score;
}
if(letterGrade=="A" || letterGrade=="B" || letterGrade=="C" ||
letterGrade=="D" || letterGrade=="F")
LetterGrade=letterGrade;
else{
System.out.println("Invalid Grade");
LetterGrade="F";
}
}
public void setScore(int score){
if(score>100){
Score=100;
} else if(score<0){
Score=0;
} else {
Score=score;
}
}
That should be everything that's relevant from my project. Here's the error message itself:
Exception in thread "main" java.lang.NullPointerException at Student.setMidterm(Student.java:19) at StudentTest.main(StudentTest.java:6)
The 19 and 6 lines are the ones I pointed out. Thanks for any help!