为什么我收到 StackOverflowError
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);
}
System.out.println
正在生成 StackOverflowError
。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您执行
toString()
时,您会调用子级的toString()
。除了在这里调用父级的toString()
之外,这里没有问题。这将调用子级的toString()
等。很好的无限循环。
摆脱它的最好方法是将您的
toString()
方法更改为:这样您就不会打印parentCategory,而只打印它的名称,没有无限循环,也没有 StackOverflowError。
编辑:正如 Bolo 下面所说,您需要检查parentCategory 是否不为空,如果是,您可能会遇到
NullPointerException
。资源:
同一主题:
When you do your
toString()
, you call thetoString()
of the children. No problem here except that you call thetoString()
of the parent in here. Which will call thetoString()
of the children, etc.Nice infinite loop.
The best way to get rid of it is to change your
toString()
method into :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 :
由于错误是
System.out.println
,因此问题必定出在toString()
中。问题是,
toString()
会打印您使用toString()
方法打印的对象的父和子Category
对象。因此,当您打印一个类别时,它会在父级上调用toString()
,在子级上调用toString()
,在子级上调用toString()
。父级对子级调用toString()
,依此类推,直到堆栈耗尽。Since the error is the
System.out.println
the problem must be in thetoString()
.The problem is that
toString()
prints both parent and childCategory
object for the one your printing using theirtoString()
method. So when you print a Category it callstoString()
on the parent which callstoString()
on the child which callstoString()
on the parent which callstoString()
on the child and so on until the stack is exhausted.因为每次调用
Category#toString()
都会产生大量其他toString
。请注意,打印childCategories
会导致打印出每个元素(通过toString
),从而重复许多toString
方法调用的整个过程。不仅如此,每个子级都会在其父级上调用
toString
,而父级又会在其子级上调用toString
,每个子级都会在父级上调用toString
,它又对其子级调用toString
,这......Because each call to
Category#toString()
produces a ton of othertoString
s. Notice that printingchildCategories
causes each element to be printed out (viatoString
), which in turn repeats this entire process of manytoString
method calls.Not only that, but each child calls
toString
on its parent, which in turn callstoString
on its children, which each callingtoString
on the parent, which in turn callstoString
on its children, which...您正在使用诸如
parentCategory
之类的实例来连接到您的 toString。它将调用这些实例的 toString 方法。toString 调用的循环永远不会结束。因为子类别将调用父类别,而父类别将再次调用子类别,所以...
不是说如果您输入: System.out.println(myObject); ,它实际上是: System.out .println(myObject.toString());
you are using instances like
parentCategory
to concat to your toString. it will call toString method of these instances.this loop of toString calls never end. because child Category will call Parent Category and Parent will call Childs again and so...
not that if you put:
System.out.println(myObject);
it actually is:System.out.println(myObject.toString());
您的
toString()
正在陷入递归混乱。您需要有两个toString()
;一份给父母,一份给孩子。您的toString
可能如下所示:Your
toString()
is going into recursive tailspin. You need to have twotoString()
; one for the parent and one for the children. YourtoString
can look like this :StackOverflowError 实际上是在构建将传递给 System.out.println 的
String
参数时引发的。每次连接
String
和Category
时,都会执行Category
的toString()
方法。问题是Category
中的toString()
有点过于冗长。解决此问题的一种方法是仅打印父类别的名称(跳过其父类别和子类别):StackOverflowError
is actually raised while building theString
argument that would be passed toSystem.out.println
.Each time you concatenate a
String
and aCategory
, thetoString()
method ofCategory
is executed. The problem is that yourtoString()
inCategory
is a bit too verbose. One way to fix it is to print only the name of the parent category (skipping it's parent and children):