StackOverflowError,找不到我在代码中进行调用的位置
我在Java中有一个StackOverflowError,它没有告诉我我自己的代码中的任何行,堆栈跟踪的相关部分是:
java.lang.StringBuilder.append(StringBuilder.java:132)
java.util.AbstractMap.toString(AbstractMap.java:523)
java.lang.String.valueOf(String.java:2838)
java.lang.StringBuilder.append(StringBuilder.java:132)
java.util.AbstractCollection.toString(AbstractCollection.java:439)
java.lang.String.valueOf(String.java:2838)
我尝试从可抛出的堆栈跟踪中获取堆栈跟踪,但由于某种原因它不包含相关节点,我试图在我的代码中找到一些关于错误所在的点。另外,我尝试将堆栈大小减小到 128k 以更早地获得错误,但无济于事。
I have a StackOverflowError in Java and it doesn't tell me any line in my own code, the relevant part of the stacktrace is:
java.lang.StringBuilder.append(StringBuilder.java:132)
java.util.AbstractMap.toString(AbstractMap.java:523)
java.lang.String.valueOf(String.java:2838)
java.lang.StringBuilder.append(StringBuilder.java:132)
java.util.AbstractCollection.toString(AbstractCollection.java:439)
java.lang.String.valueOf(String.java:2838)
I've tried getting the stack trace from the throwable, but for some reason it doesn't contain the relevant nodes, I'm trying to find some inpoint in my code as to where the error is. Also I've tried to reduce the stack size to 128k to get the error earlier, no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 StackOverFlowError,堆栈跟踪通常会被截断(因为它太长),这并不能真正帮助查找错误。
在您的情况下,假设您复制的部分是重复的部分,看起来您将一个映射添加到一个集合中,该集合又是一个作为原始映射的键或值,然后使用 toString这些对象之一上的 () 方法。
然后,地图(在 AbstractMap 中)的
toString()
的默认实现使用其所有键和值调用StringBuilder.append(...)
,这反过来又会调用toString
这些。对于 AbstractCollection,这对于集合的元素也是有效的。(Jon 的答案中的示例使用两个集合而不是地图和集合执行类似的操作。)
解决方案:
toString()
(以及hashCode
/equals
),或者toString()
方法来打破循环。For a
StackOverFlowError
, the stack trace is often truncated (since it is too long), which does not really help finding the error.In your case, assuming the part you copied is the one which is repeating, it looks like you added a map to a collection, which in turn is a as a key or value of the original map, and then use the
toString()
method on one of these objects.The default implementation of
toString()
for a map (in AbstractMap) then callsStringBuilder.append(...)
with all its keys and values, which in turn will invoketoString
on these. For AbstractCollection, the same is valid for the elements of the collection.(The example in Jon's answer does a similar thing with two collections instead of a map and a collection.)
Solutions:
toString()
(andhashCode
/equals
, too), ortoString()
method of one of them to break the cycle.下面是一个会导致相同错误的示例,可能会有所帮助:
请注意,仅使用:
... 不会产生错误。它足够聪明,可以注意到集合是否立即在其自身内,而不是嵌套在其自身内。
当然,您可能不会使用相同的集合,因此这可能会改变正在发生的事情 - 但希望这给了您一个起点。
Here's an example which will cause the same error, which may help:
Note that just using:
... did not produce the error. It's smart enough to notice if the collection is immediately within itself - but not nested within itself.
Of course, you may not be using the same collection, so that may change what's going on - but hopefully this has given you a starting point.