從類創建對象時出現錯誤。新類對象創建Java java.lang.StackOverflowError:可能的遞歸?
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();
}...
程序迭代,在Gameworld之前打印並進入下面的行然後重複。
gw = new GameWorld();
遊戲世界級
public class GameWorld extends Game {
private int gameWidth;
private int gameHeight;
public GameWorld(){
//init();
System.out.println("after inititalization");
}...
收到以下錯誤在Eclipse上運行。
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)
我在做什麼錯?爲什麼它循環這麼多?
問題不是爲什麼它循環這麼多,問題是它爲什麼循環。你的'init()'方法是什麼?我認爲他們正在創建'Game'或'GameWorld'對象,這會導致對'init()'的額外調用,等等。 –
@DavidConrad我註釋了init()方法和相同的問題。這沒有意義。 –