0

This compiles fine:

static final Screen screen = Screen.getInstance();
static final InputListener listener = InputListener.getInstance();

static
{

    screen.addListener(listener);
    screen.setCurrentState(new MainMenu());
    screen.setVisible(true);
}

private GameManager(){
    mobs = new ArrayList<Mobile>();
    player = new Player(100, 100);      
    mobs.add(player);   
}

This doesn't:

static final Screen screen = Screen.getInstance();
static final InputListener listener = InputListener.getInstance();

private GameManager(){
    mobs = new ArrayList<Mobile>();
    player = new Player(100, 100);      
    mobs.add(player);   
    
    screen.addListener(listener);
    screen.setCurrentState(new MainMenu());
    screen.setVisible(true);
}

Throws: Exception in thread "main" java.lang.ExceptionInInitializerError saying:

Caused by: java.lang.NullPointerException
at main.pack.minerdude.GameManager.<init>(GameManager.java:42)
at main.pack.minerdude.GameManager.<clinit>(GameManager.java:10)
... 1 more

Line 10 is:

private static final GameManager manager = new GameManager();

Line 42 refers to "return manager":

public static GameManager getInstance(){
    return manager;
}

Why is behaving like that if the static block gets generated after creating mob and adding player to it?

EDIT

So, this is my conclusion after executing this code and getting the following output:

GameManager constructor
Screen constructor
Screen getInstance
static of GameManager
GameManager GetInstance
  1. Some class wants a GameManager object, so to return it it has to be created first.

  2. GameManager has a Screen object declared static and has the same requeriments as the first step.

  3. Screen get's created so GameManager has it's static object initialized and continues to initialize the following statics.

  4. Now that GameManager it's fully initialized, it returns itself to the first caller.

One would assume that when GameManager getInstnace gets called, before calling it's constructor it would first initialize all it's statics, but it seems it doesn't work that way as the constructors get's called before any of it's statics. If I create a new GameManager directly - not using getInstance() - then the order is preserved: statics, variables, constructor then return.

Community
  • 1
  • 1
Jh62
  • 324
  • 1
  • 3
  • 15
  • 1
    `at ..GameManager.java:42` While 42 may be the meaning of life, the universe, and everything, that source does not contain 42 lines. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 02 '13 at 08:12
  • I only posted the conflicting code. I think that I was trying to use some variables before initializing them. I thought that when you create an object, the instance/class variables are initialized first prior to the constructor. Apparently i was wrong, because if I initialize the statis inside the constructor before assining them any value, then the code compiles fine. – Jh62 Feb 02 '13 at 08:19
  • 1
    Consider reading [this question](http://stackoverflow.com/q/10613853/597657), it might help you. – Eng.Fouad Feb 02 '13 at 08:20
  • Both of them *compile* fine, contrary to your description. It's just that one of the fails at *execution* time. – Jon Skeet Feb 02 '13 at 08:26
  • 1
    And I doubt that line 42 is really the `getInstance()` method, given the stack trace. I'd expect it to be in a constructor. – Jon Skeet Feb 02 '13 at 08:29
  • Line 42 refers to "return manager", hence the error. – Jh62 Feb 02 '13 at 08:32

1 Answers1

3

Your problem is that you are using inside your constructor static object which depends on your constructor.

The flow of your program is :

1. static final Screen screen = Screen.getInstance();
2. public static GameManager getInstance(){ return manager;}
3. private static final GameManager manager = new GameManager();
4. private GameManager(){
    //......
    screen.addListener(listener);
    //but screen is still null !!! NullPointerException !!!
}
Grisha Weintraub
  • 7,803
  • 1
  • 25
  • 45
  • There's something I don't understand: my main method creates an object named "Start" --> wich creates GameManager getInstance(); --> and then the rest get's executed. I thought that GameManager gets created, then it's class/instance variables and then the constructor gets called. – Jh62 Feb 02 '13 at 08:43
  • 1
    No, class variables are initialized first. See http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html – Grisha Weintraub Feb 02 '13 at 08:51
  • I put throughout the code some test code and this is what I get: "GameManager constructor -> Screen constructor -> Screen getInstance -> static of GameManager -> GameManager GetInstance". It looks like first it creates a new GameManager object, then creates an Screen Object then returns the screen, then initializes the statics of gameManager and then returns the GameManager object. I think I unsderstand now the order, but is a mesS! – Jh62 Feb 02 '13 at 09:06