0

I've been working on a basic class inheritance exercise, and even though I think I've got the jist of it, my program isn't working the way it should. I'm getting compile errors and haven't been able to figure out why- it'd be great if you all could help me out here.

So to start off, there are three files 1. Person.java -base class 2. Student.java -a derived class of Person.java 3. Family.java -not quite sure, I think it's its own base class

Person.java has two instance variables, String name and int age, and an assortment of constructors, toString, equals, and set/get methods

Student.java, again, a derived class of Person, by definition will have all the stuff contained within Person, as well as two more instance vars, String major, and double gpa. This class also have get/set methods for major and gpa, an equals method that compares one class student with another class student, and I believe it's called an overidden method of toString that returns name, age, major, and gpa all in one string.

Lastly, Family.java is where the main method resides. It creates an array of type Person, adds "Persons" to this array, then outputs them.

I'm fairly certain my Person.java and my Student.java is fine (I will include them at the very bottom just in case), but my problem is with Family.java. There is something wrong with how I'm adding more people to the array (addPerson method), but I just can't figure out what.


Family.java

public class Family
{
private int famSize;
private Person[] family;

public Family(int size_of_family)
{
    famSize = size_of_family;
}   
public void addPerson(Person p)
{
    boolean isPresent = false;

    int i;
    for(i = 0; i < family.length; i++)
    {

        if(family[i].equals(p) == true)
        {
            isPresent = true;
            System.out.println(p.getName() + 
            " is already present in the family");
        }                       
    }       
    if(isPresent == false)
        family[i] = p;
}
public void printOutFamily()
{
    for(int i = 0; i < family.length; i++)
    {
        System.out.println(family[i].toString());
    }
}   
public static void main(String[] args)
{
   Person fred= new Person("Fred Flintstone", 50);
   System.out.println("created " + fred);

   Person wilma = new Person("Wilma Flintstone", 48);
   Student george= new Student("George Flintstone", 21, "Politics", 3.1);
   System.out.println("created " + george);

   Student sue= new Student("Sue Flintstone", 24, "Nursing", 3.3);
   Student anotherGeorge= new Student("George Flintstone", 21, "Math", 3.4);
   Person yetAnotherGeorge= new Person("George Flintstone", 21);

   Family f = new Family(10);
   f.addPerson(fred);
   f.addPerson(wilma);
   f.addPerson(george);
   f.addPerson(sue);
   f.addPerson(anotherGeorge);
   f.addPerson(yetAnotherGeorge);

   anotherGeorge.setName("Georgie Flintstone");
   f.addPerson(anotherGeorge);

   f.printOutFamily();
}
}

Person.java

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

public Person()
{
    name = "John Smith";
    age = 1;
}

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

public String toString()
{
    return ("Name: " + getName() + ", Age: " + age + " ");
}

public boolean equals(Person otherPerson)
{
    return (getName().equals(otherPerson.getName()) && (age == otherPerson.age));
}

public String getName() 
{
    return name;
}

public void setName(String newName)
{
    name = newName;
}

public int getAge() 
{
    return age;
}

public void setAge(int newAge)
{
    age = newAge;
}
}

Student.java

public class Student extends Person
{
private String major;
private double gpa;

public Student()
{
    super();
    major = "Undecided";
    gpa = 0.0;
}

public Student(String theName, int theAge, String theMajor, double theGpa)
{
    super(theName, theAge);
    setMajor(theMajor);
    setGpa(theGpa);
}

public String toString()
{
    return ("Name: " + getName() + ", Age: " + getAge() + ", Major: " + major + ", GPA: " + gpa);
}

public boolean equals(Student otherStudent)
{
    return (major.equals(otherStudent.major) && (gpa == otherStudent.gpa));
}

public String getMajor() 
{
    return major;
}

public void setMajor(String newMajor)
{
    major = newMajor;
}

public double getGpa() 
{
    return gpa;
}

public void setGpa(double newGpa)
{
    gpa = newGpa;
}

}
8
  • 1
    What are the exact compile errors you're getting? Commented Jul 22, 2013 at 1:29
  • Exception in thread "main" java.lang.NullPointerException at Family.addPerson(Family.java:20) at Family.main(Family.java:53) Commented Jul 22, 2013 at 1:30
  • Line 20 refers to "if(family[i].equals(p) == true)" within the addPerson method, and Line 53 refers to "f.addPerson(fred);" within the main method Commented Jul 22, 2013 at 1:31
  • so yes, something is very wrong with my addPerson method, and I'm not too sure on my Family constructor either :/ Commented Jul 22, 2013 at 1:32
  • So they are not compile time errors but runtime. Commented Jul 22, 2013 at 1:32

2 Answers 2

3

You should add

family = new Person[famSize];

in your Family constructor to initialize your array. It becomes:

public Family(int size_of_family)
{
    famSize = size_of_family;
    family = new Person[famSize];
}

As @Pshemo and @MarkM noted, you also need to add a check in your if statement to make sure family[i] isn't null:

for(i = 0; i < family.length; i++)
{

    if(family[i] != null && family[i].equals(p))
    {
        isPresent = true;
        System.out.println(p.getName() + 
        " is already present in the family");
    }                       
} 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, you're right, my constructor was incomplete. Problem is, I still hit an error at the first if statement "if(family[i].equals(p) == true" within the addPerson method. Do you me asking if you know why? does the equals method not accept classes?
after initializing, the array will have null values, you need to add Person objects to the array before comparing. also, change the compare statement to this: if(true == p.equals(family[i]))
@HelloMyNameIsRay when you create new array of objects it is filled with nulls and you can't invoke any non-static methods on nulls. To solve this issue you can test if you you are trying to invoke your equals method on null with if (family[i] != null && family[i].equals(p) == true), but better way would be adding counter in your Family class, iterate with for (i = 0; i < counter; i++) and when you are adding new element do it with family[counter++] = p; instead family[i] = p. Oh, you should also test before adding if counter wouldn't be grater than size of array.
You can also add a null check to family[i] before the if statement.
0

The problem is at this line:

for(i = 0; i < family.length; i++)

at this point, family has not yet been initialized and it will be null. You have 3 options to initialize family before calling addPerson method:

1) initialize family at the time of creation

private Person[] family = new Person[5];

2) initialize family in the constructor

private Person[] family = new Person[famSize];

3) create an instance of Family and initialize family attribute before calling addPerson (this is error-prone and should be avoided)

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.