简体   繁体   English

java.lang.StackOverflowError的。为什么我在这里得到Stackoverflow异常

[英]java.lang.StackOverflowError. Why am I getting a Stackoverflow exception here

I have a list of items that are parents and children, displayed in a hierarchy. 我有一个父级和子级项目列表,以层次结构显示。 I want to toggle their expanded property. 我想切换他们expanded财产。 so if a parent is clicked and the parent's children are showing then all of the parents children will be collapsed and vice versa 因此,如果单击父级并且父级的子级正在显示,则所有父级子级将被折叠,反之亦然

Stackoverflow trace points to this line Stackoverflow跟踪指向此行

if (childItem.getItemId() == item.getItemId()) {
    hideItemAndDescendants(childItem); //causing stack overflow
  }

I under stand that a stackoverflow occurs when a method keeps calling it self infinitely .but in this case i have a For loop that only loops over the items list and the list size is only around 10. 我认为当一个方法一直无限地调用它时会发生堆栈溢出。但是在这种情况下,我有一个For循环,只循环遍历items列表,列表大小只有10左右。

public boolean toggleNodeCollapseState(long itemId) {
        boolean changed = false; // a flag to determine if anything collapsed or expanded
         for (int i = 0; i < items.size(); i++) {
            Item childItem = items.get(i);
            if (childItem.getParentItemId() == itemId) {
                changed = true;
                childItem.setCollapsed(!childItem.isCollapsed());

                if (childItem.isCollapsed()) {
                    hideItemAndDescendants(childItem);
                } else {
                    showDescendants(childItem);
                }

            }
         }
        return changed;
    }

    public void hideItemAndDescendants(Item item) {
        item.hide();
        for (int i = 0; i < items.size(); i++) {
            Item childItem = items.get(i);
            if (childItem.getItemId() == item.getItemId()) {
                  hideItemAndDescendants(childItem);
            }
        }
    }

    public void showDescendants(Item item) {
        item.hide();
        for (int i = 0; i < items.size(); i++) {
            Item childItem = items.get(i);
            if (childItem.getItemId() == item.getItemId()) {
                childItem.show();
                if (!childItem.isCollapsed()) {
                    showDescendants(childItem);
                }
            }
        }
    }

You have a recursive call in your hideItemAndDecendants: 你在hideItemAndDecendants中有一个递归调用:

for (int i = 0; i < items.size(); i++) {
    Item childItem = items.get(i);
    if (childItem.getItemId() == item.getItemId()) {
          hideItemAndDescendants(childItem);
    }
}

So if childItem.getItemId() == item.getItemId() you call hideItemAndDescendants(childItem); 因此,如果childItem.getItemId() == item.getItemId() ,则调用hideItemAndDescendants(childItem); again. 再次。 This probably causes an infinite loop causing your stackoverflowexception. 这可能会导致无限循环导致stackoverflowexception。

My guess is that you've got a data relationship which isn't a true tree - eg 我的猜测是你有一个不是真树的数据关系 - 例如

Item1
  |
  \- Item2
       |
       \- Item1

At which point it would just recurse down forever. 在这一点上,它将永远减少。 (Or more simply, an item might be its own child.) (或者更简单地说,一个项目可能是它自己的孩子。)

One way of diagnosing this would be to write some output at the start of hideItemAndDescendants to print some identifier for "this" - I'm sure you'll see a loop somewhere. 诊断这个的一种方法是在hideItemAndDescendants的开头写一些输出来打印“this”的一些标识符 - 我相信你会在某个地方看到一个循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在递归java方法中出现“线程“ main”中的异常“ main” java.lang.StackOverflowError”? - Why Am I getting “Exception in thread ”main“ java.lang.StackOverflowError” in recursive java method? 为什么我的代码中会出现java.lang.StackOverflowError? - Why am I getting java.lang.StackOverflowError in my code? 为什么我在这里得到java.lang.StackOverflowError? - why do i get java.lang.StackOverflowError here? 为什么会出现java.lang.ArrayIndexOutOfBoundsException异常? - Why am I getting java.lang.ArrayIndexOutOfBoundsException exception ? 为什么我收到java.lang.StringIndexOutOfBounds异常? - Why I am getting java.lang.StringIndexOutOfBounds Exception? 为什么我在这里得到空异常? - Why am I getting null exception here? 为什么我在构造函数中收到StackOverflowError异常 - Why am I getting a StackOverflowError exception in my constructor 需要帮助弄清楚为什么我的 TestClock.java 程序出现 java.lang.StackOverflowError - Need help figuring out why I am getting a java.lang.StackOverflowError on my TestClock.java program 为什么我会出现stackoverflow? - Why am I getting stackoverflow? 为什么我得到NoClassDefFoundError异常而不是StackOverflow错误? - Why am I getting a NoClassDefFoundError exception rather than a StackOverflow error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM