简体   繁体   中英

Java java.lang.StackOverflowError on New Class Object Creation: possible recursion?

Getting an error when creating an object from a class.

public class Game extends Form {

private GameWorld gw;

private int lives;
private int clock;

public Game() {

    // initialize game
    init();
    System.out.println("before gameworld: ");

    gw = new GameWorld();

    System.out.println("after gameworld: ");
    gw.init();
    play();
}...

Program iterates, prints before Gameworld and goes to line below then repeats.

gw = new GameWorld();

Gameworld Class

public class GameWorld extends Game {

private int gameWidth;
private int gameHeight;

public GameWorld(){

    //init();

    System.out.println("after inititalization");
}...

Getting the following error in Eclipse on run.

java.lang.StackOverflowError
at com.codename1.ui.plaf.DefaultLookAndFeel.getPreferredSize(DefaultLookAndFeel.java:675)
at com.codename1.ui.plaf.DefaultLookAndFeel.getLabelPreferredSize(DefaultLookAndFeel.java:665)
at com.codename1.ui.Label.calcPreferredSize(Label.java:484)
at com.codename1.ui.Component.preferredSizeImpl(Component.java:1985)
at com.codename1.ui.Component.preferredSize(Component.java:2020)
at com.codename1.ui.Component.getPreferredSize(Component.java:782)
at com.codename1.ui.Component.getPreferredW(Component.java:862)
at com.codename1.ui.layouts.BorderLayout.getPreferredSize(BorderLayout.java:440)
at com.codename1.ui.Container.calcPreferredSize(Container.java:1848)
at com.codename1.ui.Component.preferredSizeImpl(Component.java:1985)
at com.codename1.ui.Component.preferredSize(Component.java:2020)
at com.codename1.ui.Component.getPreferredSize(Component.java:782)
at com.codename1.ui.Component.getPreferredH(Component.java:872)
at com.codename1.ui.layouts.BorderLayout.positionTopBottom(BorderLayout.java:414)
at com.codename1.ui.layouts.BorderLayout.layoutContainer(BorderLayout.java:284)
at com.codename1.ui.Container.doLayout(Container.java:1420)
at com.codename1.ui.Container.layoutContainer(Container.java:1412)
at com.codename1.ui.Container.revalidate(Container.java:1058)
at com.codename1.ui.Toolbar$ToolbarSideMenu.initMenuBar(Toolbar.java:1233)
at com.codename1.ui.Form.setMenuBar(Form.java:3229)
at com.codename1.ui.Form.setToolbar(Form.java:3250)
at com.codename1.ui.Form.initGlobalToolbar(Form.java:218)
at com.codename1.ui.Form.<init>(Form.java:210)
at com.codename1.ui.Form.<init>(Form.java:166)
at com.mycompany.racecar.Game.<init>(Game.java:19)
at com.mycompany.racecar.GameWorld.<init>(GameWorld.java:8)
at com.mycompany.racecar.Game.<init>(Game.java:25)
at com.mycompany.racecar.GameWorld.<init>(GameWorld.java:8)
at com.mycompany.racecar.Game.<init>(Game.java:25)
at com.mycompany.racecar.GameWorld.<init>(GameWorld.java:8)
at com.mycompany.racecar.Game.<init>(Game.java:25)
at com.mycompany.racecar.GameWorld.<init>(GameWorld.java:8)
at com.mycompany.racecar.Game.<init>(Game.java:25)
at com.mycompany.racecar.GameWorld.<init>(GameWorld.java:8)
at com.mycompany.racecar.Game.<init>(Game.java:25)
at com.mycompany.racecar.GameWorld.<init>(GameWorld.java:8)
at com.mycompany.racecar.Game.<init>(Game.java:25)
at com.mycompany.racecar.GameWorld.<init>(GameWorld.java:8)
at com.mycompany.racecar.Game.<init>(Game.java:25)

What am I doing wrong? Why is it looping so much?

You get java.lang.StackOverflowError exception because of an infinite loop in creating of a Game object. This is the sequence of calls that falls in loop and cause StackOverflowError :

new Game() => new GameWorld() => new Game() ....

In Java, a child class always calls its parent constructor implicitly or by calling super() explicitly. So GameWorld constructor will call the Game default constructor and then it calls the GameWorld constructor again and it goes forever.

GameWorld extends Game.

So each time the child ctor is called it calls its super class ctor.

Case solved.

Your call to new GameWorld calls new Game implicitly because the compiler inserts a call to super() into your constructors.

Beyond that : A extends B means A IS a B.

But a GameWorld is NOT a Game! So this also wrong from a modeling perspective!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM