Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:38:31+00:00 2026-06-18T04:38:31+00:00

This compiles fine: static final Screen screen = Screen.getInstance(); static final InputListener listener =

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:38:32+00:00Added an answer on June 18, 2026 at 4:38 am

    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 !!!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this
This compiles fine in Visual studio, but why not in XCode? class A() {};
Consider this: std::vector<int*> v(1, 0); This compiles fine with VC++10 (no warnings even at
This code compiles fine: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances, FlexibleContexts, EmptyDataDecls, ScopedTypeVariables, TypeOperators,
This code compiles fine with GCC and Clang but not with MSVC 2010: #include
I'm having a hard time using std::string::iterators in C++. This code compiles fine (still
This Java code compiles fine, but when I try to run it I get:
This confuses me. The following compiles fine under Eclipse. package com.example.gotchas; public class GenericHelper1
This typedef: typedef DWORD WINAPI (* CM_Open_DevNode_Key)(DWORD, DWORD, DWORD, DWORD, PHKEY, DWORD); compiles fine
I wrote this code that compiles on Solaris gcc it works fine too for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.