Solved error when trying to put player to list

Discussion in 'Plugin Development' started by deivisxm, Mar 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    deivisxm

    Hi. I am trying to make rpg party invite on click, it should add player to list, but it throws null pointer exception . Here is the code :
    Code:
    if (event.getInventory().getName().contains(" Interactions")) {
        Player pl=Bukkit.getPlayer(this.getName(event.getInventory().getName()));
        if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("Invite to party")){
            if(PartyInvite.containsKey(p)){
                if(!PartyInvite.containsValue(pl)){
                  AcceptMenu(pl, p);
                  ArrayList list=PartyInvite.get(p);
                  list.add(pl.getName());
                  p.sendMessage(pl.getName());
                PartyInvite.get(p);
                }
            }else{
                PartyInvite.put(p, null);
                p.sendMessage(ChatColor.RED+"You are already in a party...");
            }
        }
        p.closeInventory();
    }
    Code:
    public final HashMap<Player, ArrayList<String>> PartyInvite = new HashMap<Player, ArrayList<String>>();  
    
    I just started on parties so this dont really make any sense, but the problem is in this line
    Code:
    list.add(pl.getName());
    
     
  2. Offline

    Garris0n

    1. You shouldn't be saving player instances like that. Either save the name or use a weak map.
    2. You should not have a variable named "PartyInvite". Read this.
    3. You have to initialize a list for each player in the map if one does not already exist. If you never created one and added it to the map, what else would it be except null?
     
  3. Offline

    deivisxm

    well i am initialising it for each player ArrayList list=PartyInvite.get(p);
     
  4. Offline

    Minesuchtiiii

    You should use a string list!
    ArrayList<String> PartyInvite = new ArrayList<String>();
    and save p.getName();
     
  5. Offline

    Garris0n

    That's getting it for each player. The thing is, you never put anything there in the first place.
     
  6. Offline

    deivisxm

    Thank you! understood my mistake :)
     
Thread Status:
Not open for further replies.

Share This Page