编程语言
首页 > 编程语言> > java – 有关堆栈溢出错误的帮助?

java – 有关堆栈溢出错误的帮助?

作者:互联网

这是我得到的错误:

java.lang.StackOverflowError
    at apple.awt.CGraphicsDevice.getScreenInsets(Native Method)
    at apple.awt.CGraphicsDevice.getScreenInsets(CGraphicsDevice.java:673)
    at apple.awt.CToolkit.getScreenInsets(CToolkit.java:741)
    at java.awt.Window.init(Window.java:394)
    at java.awt.Window.<init>(Window.java:432)
    at java.awt.Frame.<init>(Frame.java:403)
    at java.awt.Frame.<init>(Frame.java:368)
    at javax.swing.JFrame.<init>(JFrame.java:158)
    at D3D.<init>(D3D.java:35)
    at player.<init>(player.java:1)
    at D3D.<init>(D3D.java:17)
    at player.<init>(player.java:1)

这是玩家类:

public class player extends D3D
{
  int playerX, playerY;
  boolean east, west, south, north;
  public void setPlayer()
  {
    playerX = 1; playerY = 1;
    east=true; west=false; north=false; south=false;
  }
}

这是D3D类:

public class D3D extends JFrame
{
  player player = new player();
  mapgeneration levelmap = new mapgeneration();
  boolean ONE, TWO, THREE, FOUR, FIVE;
  boolean ONEhighlight,TWOhighlight,THREEhighlight,FOURhighlight,FIVEhighlight;
  Timer timer = new Timer(250,new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
      repaint();
    }
  });

  String tracer;

  Image Example = Toolkit.getDefaultToolkit().getImage("images/example.png");
  Image Startup = Toolkit.getDefaultToolkit().getImage("images/Startup.png");
  Image ButtonHighlight = Toolkit.getDefaultToolkit().getImage("images/ButtonHighlight.png");

  public D3D()
  {
    super();
    setSize(342,277);

      ...
    JPanel main = new JPanel()
    {
      public void paintComponent(final Graphics g)
      {
        super.paintComponent(g); 
        timer.start();
        g.drawImage(Startup,0,0,this);
        ...
      };
    };
    add(main);
  }
  public void init()
  {
    player.setPlayer();
    levelmap.populateGraph();
  }
  public static void main(String[] args)
  {
    D3D game = new D3D();
    game.setTitle("Dungens:3D");
    game.init();
    game.setVisible(true);
  }
}

我已经看了几个小时,并把它缩小到你在这里看到的.说实话,我可能会看到一些愚蠢的小东西.

多谢你们.

解决方法:

我看到你的问题.您的Player类扩展了您的D3D GUI – 导致一种疯狂的循环引用.当您循环地继续创建玩家和D3D对象时,这将导致递归.

为了证明我是对的,只需运行这个非常简单的代码版本:

public class D3D {
   Player player = new Player();

   public static void main(String[] args) {
      D3D game = new D3D();
   }
}

class Player extends D3D {

}

因此,当创建D3D对象时,它会创建一个Player对象,并且由于它扩展了D3D,它会创建另一个Player对象,因为它是一个D3D对象,所以创建另一个Player对象,然后依次打开.

但即使它没有引起递归,玩家也不应该扩展GUI,因为玩家不是gui;它们是完全不同的逻辑结构.

标签:java,stack-overflow
来源: https://codeday.me/bug/20190903/1797471.html