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.
StackOverflowError is a runtime error which points to serious problems that cannot be caught by an application. The java. lang. StackOverflowError indicates that the application stack is exhausted and is usually caused by deep or infinite recursion.
The toString() method returns a string representing the specified string value.
We can override the toString() method in our class to print proper output. For example, in the following code toString() is overridden to print the “Real + i Imag” form.
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 + "]";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With