Object is null even though I initialized it on creation!

So, I was making a game state system, and came to this:


Exception in thread "main" java.lang.NullPointerException
	at com.wessles.MERCury.StateCore.addState(StateCore.java:53)
	at com.wessles.horde.Horde.init(Horde.java:46)
	at com.wessles.MERCury.Runner.boot(Runner.java:43)
	at com.wessles.MERCury.Core.<init>(Core.java:39)
	at com.wessles.MERCury.StateCore.<init>(StateCore.java:29)
	at com.wessles.horde.Horde.<init>(Horde.java:34)
	at com.wessles.horde.Horde.main(Horde.java:60)

I looked at the code, and for some reason this is null:


	public void addState(GameState state) {
		gamestates.put(states, state);
		states++;
	}

Even though I initialized gamestate on the class:


	private HashMap<Integer, GameState> gamestates = new HashMap<Integer, GameState>();

So it was initialized, and yet it is null (I checked with (gamestates == null)).
Why?!

EDIT:
I know I could just check for null and then initialize it, but I NEED to know how this could happen.

Also:
Full code

Is it the gamestates thats null, could it be the specific GameState you are passing it, that is null?

Are you sure that states Integer is initialized?

Thats an abstract class. What class are you using to extend/implement it? What about the code? Maybe you are ‘nulling’ it somewhere else.

Can you even call ‘new’ (initialize) like that in an abstract class? hmmmmm?

I agree with namrog, Wesley. I’d be looking at the non-abstract subclass.

I’m getting a bit “out there”, but:

Did you override the addState() method in the subclass??? Because the only way I can replicate your error is to override addState(). As gamestates is private, it is not visible to your subclass, and as such would force your subclass to have it’s own gamestates HashMap if it was to work properly. I wonder if this has happened, and you have not initialized gamestates in the subclass??? …Just throwing it out there…

Other than that, the only other possibility I can think of is that you have nullified it somewhere, as has already been pointed out.

Regardless, I think the answer lies in your non-abstract implementation. So scrutinise that, or post it up here.

Cheers,
nerb.

Try changing your declaration to private final HashMap and see if you get any errors compiling that set the value to null.

Other thing to check is see if your subclass is throwing the exception.

Fairly sure from your stacktrace that the problem is in the superclass. Seems that the method is called during the constructor of Core (via Runner, Horde)? You can’t do this. Variables are not initialised until after the superclass constructor completes.

I was just going to say the same thing as nsigma, the error is visible in the stack trace.

This method,

com.wessles.MERCury.Core.<init>(Core.java:39)

is run before your hashmap is initialized.

Learn to use a debugger; it makes issues like this trivial to solve.

When the uncaught exception is thrown, the jvm will halt allowing you to examine the stack and determine the exact cause of the problem.