Solved HashMap givin NullPointerException

Discussion in 'Plugin Development' started by Johnny Crazy, Sep 25, 2012.

Thread Status:
Not open for further replies.
  1. Hi, I am here again...

    I have Problems with my HashMap, when i want to get the HashMap (getChat()) it gives me a NullPointer, but when i use the HashMap in the Class, it works fine.

    Gama.java
    Code:java
    1.  
    2. HashMap<String, Integer> chat = new HashMap();
    3.  
    4.  
    5.  
    6.  
    7.  
    8. chat.put(p.getPlayer().getName(), ID);
    9. System.out.println(chat.get(p.getPlayer().getName()));
    10. //This works perfect...
    11.  
    12.  
    13. public HashMap<String , Integer> getChat()
    14. {
    15. return chat;
    16. }
    17.  
    18.  



    Event.java
    Code:java
    1.  
    2.  
    3. HashMap<String, Integer> chat = new HashMap<>();
    4. //tried it without creating a new one (!?), didnt work though;
    5.  
    6. chat = game.getChat(); //HERE is the NullPointer
    7.  



    What did i do wrong?
     
  2. Offline

    andf54

    game is null?

    NullPointer mean that something is null. Just do System.out.println(variable/field/etc) until you find our what is null on the given field.
     
  3. i know what a NullPointer is...but game/chat is not null...

    and btw chat isnt null because i get the System.out.print correctly
     
  4. Offline

    andf54

    Something has to be null if the stack trace points to game.getChat(); While getChat() can return a null, it would not throw a NPE. So game must be null.
     
  5. Game is just a reference to an other Class

    import com.Johnny.VIP.Game and Game game;

    Also, Eclipse gives me all Methods from Class Game....
     
  6. Offline

    Hoolean

    I think the code for making HashMaps is:
    Code:java
    1. HashMap<String, Integer> name = new HashMap<String, Integer>();
     
  7. The HashMap works fine in Class Game.java

    But when i want to get it through getChat() and save it to the hashMap "chat", its a Nullpointer...
     
  8. Offline

    andf54

    Then "Game game" in never initialised or initialised as null.

    Based on what you gave me I can only deduce that game is null. Paste your whole stacktrace and source

    As far as I understand, the compiler doesn’t really care what you put in a HashMap. <String, Integer> is so that the programmer can’t do stupid mistakes when coding.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  9. andf54

    Oh...i was editing the Source of a Plugin not made by me, and there were severeal "Game" Instances.

    Now its working,

    Thanks !! ;)
     
Thread Status:
Not open for further replies.

Share This Page