简体   繁体   中英

Error when running code through command line

I have this code:

public class PlayGame
{

public static void main(String[] args)
{
    PlayGame p = new PlayGame();
    p.startup();
}


PlayGame p = new PlayGame();
GameLogic g = new GameLogic();

(With correct formatting). The startup class just has some code which I know works fine. Its just printing out stuff. However when running I get a load of errors

at PlayGame.<init>(PlayGame.java:13)

This is repeated a lot and is the only thing I see in command line. I have found by inserting

System.exit(0)

between lines to exit before the error appears that it is in the

PlayGame p = new PlayGame();

line. I have no idea why it is so any help is much appreciated. Thank you

The PlayGame class has an instance field

PlayGame p = new PlayGame();

So each PlayGame object creates a PlayGame object, which creates a PlayGame object, ad nauseam.

You'll eventually run out of memory with a StackOverflowError .

Why do you need an instance of PlayGame within an instance of PlayGame ?

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