Results 1 to 2 of 2

Thread: Help with NullPointerException runtime error.

  1. #1
    Join Date
    Feb 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with NullPointerException runtime error.

    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:
    java.lang.NullPointerException
    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.
    Thanks.

  2. #2
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up The Error

    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

    if(.....!=null)
    {
    ...
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •