简体   繁体   English

帮助堆栈溢出错误?

[英]Help with stack-overflow error?

Here's the error I'm getting: 这是我得到的错误:

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)

And here's the player class: 这是玩家类:

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;
  }
}

And here's the D3D class: 这是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);
  }
}

I've been looking at this for hours and have narrowed it down to what you see here. 我已经看了几个小时,并把它缩小到你在这里看到的。 To be honest, it's probably some stupid little petty thing that I'm glancing over. 说实话,我可能会看到一些愚蠢的小东西。

Thanks guys. 多谢你们。

I see your problem. 我看到你的问题。 Your Player class extends your D3D GUI -- causing a kind of crazy circular referencing. 您的Player类扩展了您的D3D GUI - 导致一种疯狂的循环引用。 This will cause a recursion as you cyclically keep creating players and D3D objects. 当您循环地继续创建玩家和D3D对象时,这将导致递归。

To prove that I'm right, just run this very simple version of your code: 为了证明我是对的,只需运行这个非常简单的代码版本:

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

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

class Player extends D3D {

}

So when the D3D object is created, it creates a Player object, and since it extends D3D, it creates another Player object which in turn since it's a D3D object, creates another Player object, and on and on. 因此,当创建D3D对象时,它会创建一个Player对象,并且由于它扩展了D3D,它会创建另一个Player对象,因为它是一个D3D对象,所以创建另一个Player对象,然后依次打开。

But even if it didn't cause recursion, Player should never extend the GUI since a player is not a gui; 但即使它没有引起递归,玩家也不应该扩展GUI,因为玩家不是gui; they're totally different logical constructs. 它们是完全不同的逻辑结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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