简体   繁体   中英

Stack Overflow Error with toStrings() (java)

Two of my toString()'s appear to be in an infinite loop, but I am unsure what the problem is. I have been debugging this for hours now and can't see anything that would be wrong.

For context, the pertinent parts of the main are: System.out.println(class1); adminStaff1.assignInstructor(class1, instructor1); System.out.println(class1);

The first println works, but when I debug I see one source lookup error before it goes through. The second class1 creates the StackOverflowError which is:

Exception in thread "main" java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at java.lang.StringBuilder.<init>(Unknown Source)
at Instructor.toString(Instructor.java:59)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at Class.toString(Class.java:89)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at java.util.AbstractCollection.toString(Unknown Source)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at Instructor.toString(Instructor.java:59)

The toString for Class is:

@Override
public String toString() {
    return "Class [instructor=" + instructor + ", lectureHall="
            + lectureHall + ", currentEnrollment=" + currentEnrollment
            + ", timeSlot=" + timeSlot + ", filled=" + filled
            + ", studentList=" + studentList + "]";
}

The toString for Instructor is:

@Override
public String toString() {
    return "Instructor [salary=" + salary + ", classList=" + classList
            + ", openTimeSlots=" + openTimeSlots + ", "
            + super.toString() + "]";
}

and the super.toString() is:

@Override
public String toString() {
    return "[idNumber=" + idNumber + ", email=" + email
            + "]";
}

I'm not sure why, but adminStaff1.assignInstructor also seems to be contributing to the problem, it is:

public void enrollStudent(Student student, Class aClass){
    aClass.checkIfFilled();
    if(!aClass.getFilled()){
        aClass.addStudent(student);
        student.addClass(aClass);
        aClass.increaseEnrollment();
    } else {
        System.out.println("Could not add student because the class is currently full.");
    }
}

I sincerely appreciate if anyone can help me understand what is wrong. Thanks.

When you call toString() on a Class it includes the Instructor (and the Instructor includes the Class ). This is a cycle. Break it, either

return "Instructor [salary=" + salary + /* ", classList=" + classList */
        + ", openTimeSlots=" + openTimeSlots + ", "
        + super.toString() + "]";

Or,

return "Class [lectureHall=" + lectureHall + ", currentEnrollment=" 
        + currentEnrollment + ", timeSlot=" + timeSlot + ", filled=" + filled
        + ", studentList=" + studentList + "]";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM