简体   繁体   中英

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:

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. This will cause a recursion as you cyclically keep creating players and D3D objects.

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.

But even if it didn't cause recursion, Player should never extend the GUI since a player is not a gui; they're totally different logical constructs.

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