Error with toStrings () (java)

Two of my toString () end up in an infinite loop, but I'm not sure what the problem is. I've been debugging this for hours and I don't see anything that would be wrong.

In context, the relevant parts of the main: 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 going through it. The second class1 throws a StackOverflowError which:

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 class for the class:

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

      

ToString for instructor:

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

      

and super.toString ():

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

      

I'm not sure why, but adminStaff1.assignInstructor also seems to contribute to the problem, this:

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 understand if anyone can help me figure out what is wrong. Thank.

+3


source to share


1 answer


When you call toString()

on Class

, it includes Instructor

(and Instructor

includes Class

). This is a cycle. Break it down, either

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

      



Or

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

      

+4


source







All Articles