javadata-structuresstack-overflowoctree

Octree StackOverflowError


Good day, I am implementing an Octree. However, I kept receiving a StackOverflowError when having an input size greater than 2. I am clueless on why I am getting this error.

Instantiation of Octree

octree = new Octree(3, new Point(0,0,0));

where the first parameter is the dimension and the second parameter is the root

Code for the population of the Octree:

public void generateList(int num)
{
    for(int i = 0 ; i < num; i++)
        for (int j = 0 ; j < num; j++)
            for (int z = 0 ; z < num; z++)
                if (!(z == j && j == i))  {
                    octree.add_nodes(i,j,z);
                }
}

Code for the add_nodes of the Octree

public boolean add_nodes(double x, double y, double z) {

    boolean success = false;
    System.out.println(x+","+y+","+z);
    if (this.i < 8) {
        if (compare(x, y, z)) {
            if (root.childPoints == null) {
                System.out.println("Root is null" + x + " " + y + " " + z);
            }

            this.root.childPoints[i] = new Point(x, y, z);
            this.i++;
            success = true;
        }
    }

    else if (this.childNodes == null) {

        this.create_nodes_of_nodes(x, y, z);

        for (int j = 0; j < 8; j++) {
            double tempx = this.root.x - root.childPoints[j].x;
            double tempy = this.root.y - root.childPoints[j].y;
            double tempz = this.root.z - root.childPoints[j].z;

            int checker = compareValues(tempx, tempy, tempz);

            this.childNodes[checker].add_nodes(root.childPoints[j].x,
                    root.childPoints[j].y, root.childPoints[j].z);

        }
        root.childPoints = null;

        double tempx = this.root.x - x;
        double tempy = this.root.y - y;
        double tempz = this.root.z - z;
        int checker = compareValues(tempx, tempy, tempz);
        this.childNodes[checker].add_nodes(x, y, z);
        // this.i=0;
    }

    else {

        if (childNodes != null) {
            int checker = compareValues(x, y, z);
            childNodes[checker].add_nodes(x, y, z);

        }
    }

    return success;
}

Errors:

Exception in thread "main" java.lang.StackOverflowError
at sun.misc.FloatingDecimal$BinaryToASCIIBuffer.appendTo(FloatingDecimal.java:307)
at sun.misc.FloatingDecimal.appendTo(FloatingDecimal.java:89)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:709)
at java.lang.StringBuilder.append(StringBuilder.java:226)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:73)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:97)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)
and an additional 400 lines of at 
edu.modesta.test.octree.Octree.add_nodes(Octree.java:107) error

Hope someone could answer. Thanks!


Solution

  • I'm not 100% certain because I can't see the rest of the code, but it looks like at some point you entered add_nodes() when i >= 8. From then on, your control flow says

    if (this.childNodes == null), do some stuff then call add_nodes(),

    else if (childNodes != null), do some stuff then call add_nodes().

    Either way, you're calling add_nodes() if i isn't less than 8, and since you never decrement i, each subsequent call is going to call add_nodes() too.

    Each time you call a method, the new method's frame gets added on the stack. A StackOverflowException happens when eventually that stack gets too big. Since you hypothetically call add_nodes() infinite times, each call grows the stack a little until it gets too big, hence your Exception.

    As an aside: I'm a bit confused why you refer to childNodes as this.childNodes and also just childNodes. this is typically only necessary if you have two variables with differing scopes but the same name, which you don't have here. You might want to brush up on using this as well.