Results 1 to 5 of 5
  1. #1
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default HashMap Null error

    Hi. I have a HashMap which maps a String to a JLabel:
    Java Code:
    tileSlots.put("slot0", slot0);
    But later on in the codes when I try to get the JLable via the HashMap, it gives me the "NullPointerException" exception error.
    Error at:
    Java Code:
    JLabel x = tileSlots.get("slot"+i);
    print(x.getName());
    Any idea wats wrong?

    P.S:- tileSlots is declared as public and the print line is where the error points to. The code where the error occurs is in a loop, thats why am using "i" variable.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: HashMap Null error

    The code looks fine. Perhaps you are using two different Maps (one local Map with the same name :D)?

  3. #3
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: HashMap Null error

    I just checked and am not using a local map. The "tileSlots" var is in green as am using Netbeans as SDK. Would you like the whole IF statement if it might help? (thnx for the quick reply though )

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: HashMap Null error

    Yes, maybe a little bit more code would help.

  5. #5
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: HashMap Null error

    There:
    Java Code:
            if (data.contains("Tile") && data.contains("L")) {
                dominogameServer.Tile[] tileSet = (dominogameServer.Tile[]) input;
                
                for(int i=0;i<tileSet.length;i++){            
                    tiles.add(new Tile(tileSet[i].getTileNum())); // 'tiles' type is 'ArrayList'
    JLabel x = tileSlots.get("slot"+i);
    print(x.getName());
                    showIcon(x, tileSet[i].getTileNum());
                }
                
                for (int i=0;i<tileSet.length;i++){
                    print("tile: " + tileSet[i].getTileNum());
                }
    
            }
    Last edited by anoorally; 02-12-2012 at 09:56 AM.

Similar Threads

  1. Null error Thread
    By anoorally in forum New To Java
    Replies: 3
    Last Post: 02-08-2012, 11:44 PM
  2. null pointer error help!!
    By fakepics500 in forum New To Java
    Replies: 1
    Last Post: 07-16-2011, 02:57 PM
  3. HashMap assignment and null pointer exception
    By merik in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 05:18 AM
  4. help with with my null error
    By zhangster in forum New To Java
    Replies: 3
    Last Post: 03-20-2010, 12:32 AM
  5. Null Error
    By scoleman123 in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM

Posting Permissions

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