[Solved] Not sure why I am getting a StackOverflowError. Also have a yellow underline under Vector, and 1 other place in code


Calling setbounds from your reshape method would be causing stackoverflowerror.

If you look at the source code of Component class the setBounds method calls the reshape method. So from your reshape method when you call super (Component) class setbounds method then from this Component setbounds method again your reshape overridden method is called which is why there is a recursive call and hence statckoverflowerror. To fix this you need to call super.reshape(…) instead of super.setBounds(..)

A sample code demonstrating this is given below:-

public class MyClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyClassB obj = new MyClassB();
        obj.reshape(0, 2, 4, 6);
    }

}

class MyClassA {

    public void setBounds(int x,int y,int width,int height) {
        reshape(x, y, width, height); // This calls the child class reshape method if it is overridden and hence can be recursive call

    }

    public void reshape(int x,int y,int width,int height) {

    }
}

class MyClassB extends MyClassA {

    public void reshape (int x, int y, int w, int h) {
        super.setBounds(x, y, w, h);
    };

}

5

solved Not sure why I am getting a StackOverflowError. Also have a yellow underline under Vector, and 1 other place in code