X753: Null Pointer Exception Practice 2

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 3 people and return the true if Anne is the oldest.

The three people are:

  • Anne, age 15, grade B
  • Franky, age 6, grade A
  • Ben, age 10, grade F

Below is their attempt, but a null pointer exception is still being thrown! 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.