View Full Version : Help with NullPointerException runtime error.

02-08-2008, 01:43 AM
I'm writing a java program and have completed one section which compiles fine, but then throws a NullPointerException at runtime and I cannot figure out why or how to solve it. I'm using a HashMap and trying to use it's put(K,V) method where it throws the error.

I have declared my HashMap at the start of the class as follows:
private HashMap<String, Item> allItems;

I have contructed it as follows:
allItems = new HashMap<String, Item>(30);

Then I use it in the method as follows:
allItems.put("armour", armour);
allItems.put("sword", sword);
allItems.put("darkcorner", darkcorner);

Item is my own class and the above items armour, sword and darkcorner have been constructed earlier in that method and seem fine seeing because it all compiles.

The runtime error occurs on the first line of my method as I have shown it above and gives me the following information:
at Game.createRoomsAndItems(Game.java:157)
at Game.<init>(Game.java:48)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at bluej.runtime.ExecServer$3.run(ExecServer.java:808)

Any help would be very much appreciated.

08-13-2009, 12:07 PM
Null pointer error happens when u use a pointer whose value is null -or with being defined first-
So u better make this chek before using any pointer