X752: Null Pointer Exception Practice 1

For the question below, assume the following implementation of a Person class:

public class Person {
    private int age;
    private String name;
    private String grade;

    public Person(int a, String n, String gr) {
        age = a;
        name = n;
        grade = gr;
    }

    public int getAge() {
        return age;
    }


    public String getName() {
        return name;
    }


    public String getGrade() {
        return grade;
    }

}

Your coworker wanted to create a method that would create 2 people and return the true if Anne is the oldest.

The two people are:

  • Anne, age 15, grade B
  • Ben, age 10, grade F

Below is their attempt, but it is throwing a Null Pointer exception. Fix the issue with the code and think about how you would explain the problem to your coworker.

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.