public class Category {
private Category parentCategory;
private Set<Category> childCategories;
private String name;
public Category() {
childCategories = new HashSet<Category>();
}
public Category getParentCategory() {
return parentCategory;
}
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}
public Set<Category> getChildCategories() {
return childCategories;
}
public void setChildCategories(Set<Category> childCategories) {
this.childCategories = childCategories;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category [childCategories=" + childCategories + ", name="
+ name + ", parentCategory=" + parentCategory + "]";
}
}
public static void main(String[] args) {
Category books = new Category();
books.setName("Books");
books.setParentCategory(null);
Category novels = new Category();
novels.setName("Novels");
novels.setParentCategory(books);
books.getChildCategories().add(novels);
//novels.setChildCategories(null);
System.out.println("Books > " + books);
}
The System.out.println
is generating the StackOverflowError
.
The most common cause of StackOverFlowError is excessively deep or infinite recursion. In Java: There are two areas in memory the heap and stack.
StackOverflowError is an error which Java doesn't allow to catch, for instance, stack running out of space, as it's one of the most common runtime errors one can encounter.
Stack overflow means, that you have no room to store local variables and return adresses. If your jvm does some form of compiling, you have the stackoverflow in the jvm as well and that means, you can't handle it or catch it.
When you do your toString()
, you call the toString()
of the children. No problem here except that you call the toString()
of the parent in here. Which will call the toString()
of the children, etc.
Nice infinite loop.
The best way to get rid of it is to change your toString()
method into :
@Override
public String toString() {
return "Category [childCategories=" + childCategories + ", name="
+ name + ", parentCategory=" + parentCategory.getName() + "]";
}
This way you don't print the parentCategory but only its name, no infinite loop, no StackOverflowError.
EDIT: As Bolo said below you will need to check that parentCategory is not null, you might have a NullPointerException
if it is.
Resources :
On the same topic :
Since the error is the System.out.println
the problem must be in the toString()
.
The problem is that toString()
prints both parent and child Category
object for the one your printing using their toString()
method. So when you print a Category it calls toString()
on the parent which calls toString()
on the child which calls toString()
on the parent which calls toString()
on the child and so on until the stack is exhausted.
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