Java ArrayList StackOverFlowError

时间:2016-03-13 15:01:24

标签: java arraylist stack-overflow

这是我第一次提问,请耐心等待。我正在完成一项学校任务,并且一直在与一个我不太了解的堆栈溢出错误进行斗争。错误由~~~:

表示

堆栈跟踪的代码部分如下:

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);
    }

3 个答案:

答案 0 :(得分:0)

您正在进行对象的循环实例化,这会导致堆栈溢出。

Staff构造函数中,您要实例化许多类,例如ExecutiveEmployee。您的Executive课程extends Employee课程和Employee课程extends您的StaffMember课程。

当你这样做时,Staff的构造函数将再次被隐式地调用(通过从super()调用Executive,然后Employee和然后StaffMember)并重复整个过程,从而消耗你所有的记忆。 摆脱你的班级extends Staff可能会解决你的问题(可能有问题的是来自extends的{​​{1}},这是调用链中的最后一个。)

答案 1 :(得分:0)

有一个构造函数调用的循环链。 您的基类构造函数jdbcTemplate正在实例化子类,而子类又调用自构造函数,导致无限链导致Staff()错误。

此外,stackOverFlow扩展StaffMember似乎不正确。由于员工是由所有员工组成的全体员工,Staff代表一名员工。两者之间没有StaffMember的关系。

您应该从StaffMember中删除is A,代码也可以正常工作。

答案 2 :(得分:0)

类A的构造函数调用类B的构造函数。类B的构造函数调用类A的构造函数。您有一个无限递归调用,这就是为什么你最终得到StackOverflowError的原因。您将进入构造函数的循环调用。

Java支持在类之间存在循环依赖关系,这里的问题只与构造函数相互调用有关。