user3344370
user3344370

Reputation:

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

Upvotes: 0

Views: 30

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280132

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?

Upvotes: 1

Related Questions