Search code examples
javaarrayliststack-overflow

Java ArrayList StackOverFlowError


This is my first time asking a question so please be patient. I am working on a school assignment and have been battling a stackoverflow error that I do not know enough about to get to the bottom of. The errors are indicated by ~~~:

The sections of code from the stack trace are as follows:

public class Employee extends StaffMember {
    public static final int DEFAULT_SIN = 123456789;
    public static final double MINIMUM_WAGE = 400.00;
    protected int socialInsuranceNumber = DEFAULT_SIN;;
    protected double payRate = MINIMUM_WAGE;

    /**
     * 
     */
    public Employee() {
        super();
    }

    /**
     * @param name
     * @param streetAddress
     * @param phone
     * @param socialInsuranceNumber
     * @param payRate
     */
    public Employee(String name, String address, String phone, int socialInsuranceNumber, double payRate) {
    ~~~ super(name, address, phone);
        this.socialInsuranceNumber = socialInsuranceNumber;
        this.payRate = payRate;
    }
public abstract class StaffMember extends Staff {
    public static final String DEFAULT_NAME = "Default Name";
    public static final String DEFAULT_ADDRESS = "Default Address";
    public static final String DEFAULT_PHONE = "Default Phone";

    protected String name;
    protected String address;
    protected String phone;

    /**
     * default constructor
     */
    public StaffMember() {
        super();
    }

    /**
     * 
     * @param name
     * @param address
     * @param phone
     *            abstract class can not be instantiated therefore should not
     *            have constructors
     */

    ~~~ public StaffMember(String name, String address, String phone) {
        super();
        this.name = name;
        this.address = address;
        this.phone = phone;
    }
public class Staff {

    public ArrayList<StaffMember> staffList;

    /**
     * Constructor for objects of type Staff.
     */
    public Staff() {

        staffList = new ArrayList<StaffMember>(6);

    ~~~ staffList.add(new Executive("Hilary", "203 Whitewater Line", "871-0469", 123456789, 5000, 0));
        staffList.add(new Employee("Thomas", "1000 Robson Street", "604-0000", 010203040, 1500));
        staffList.add(new Hourly("Condoleeza", "678 Fifth Ave.", "905-0690", 958473625, 18.50, 0));
        staffList.add(new Volunteer("Kimberly", "1200 West Point Grey Road", "514-8374"));
        staffList.add(new Volunteer("Jean", "321 Shawinigate Lane", "613-7282"));

    }
public class Executive extends Employee {

    private double bonus;

    /**
     * Default Constructor
     */
    public Executive() {
        super();
    }

    /**
     * @param name
     * @param address
     * @param phone
     * @param socialInsuranceNumber
     * @param payRate
     */
    public Executive(String name, String address, String phone, int socialInsuranceNumber, double payRate, double bonus) {
        super(name, address, phone, socialInsuranceNumber, payRate);
        this.awardBonus(bonus);
    }

Solution

  • There is a Circular chain of constructor invocations. Your base class constructor Staff() is instantiating child classes which in turn invoke self constructor leading to infinite chain causing stackOverFlow error.

    Also, it doesn't seem correct for StaffMember to extend Staff . As staff is the entire staff consisting of all the employees and StaffMember represents a single employee. There is no is A relationship between the two.

    You should remove the extends Staff from StaffMember and the code should also work fine then.