0

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!

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
user3304654
  • 65
  • 1
  • 1
  • 6
  • `Midterm` doesn't seem to have ever been initialised – MadProgrammer Sep 02 '15 at 04:10
  • Also, this `letterGrade=="A"` is not how `String` comparison works in Java, you should be using something more like `"A".equals(letterGrade)` – MadProgrammer Sep 02 '15 at 04:11
  • You might also like to have a read through [Code Conventions for the Java TM Programming Language](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html), it will make it easier for people to read your code and for you to read others – MadProgrammer Sep 02 '15 at 04:11
  • Okay thank you. So I need to do "private Exam Midterm = new Exam(int, String) instead? – user3304654 Sep 02 '15 at 04:14
  • You `Exam` inherits the default constructor from `Object`, so you don't "have" to write one, unless you have some special setup/intiialisation you need to carry out...but yes, you should use `private Exam Midterm = new Exam();` – MadProgrammer Sep 02 '15 at 04:15

0 Answers0