HashMap returns null, while it is set

Discussion in 'Plugin Development' started by blackwolf12333, May 28, 2012.

Thread Status:
Not open for further replies.
  1. Hi, I have a problem with my hashmap, i have created a hashmap like this:
    Code:
    public HashMap<String, String> playersWithInstandColor = new HashMap<String, String>();
    then i put a value in it this way:
    Code:
    playersWithInstandColor.put(p.getName(), "blue");
    p is here a player.
    thats in the onCommand function of my plugin, but when i try to use it somewhere else, like this:
    Code:
    playersWithInstandColor.get("blackwolf12333");
    That will be null, and i have no idea why, because i set it in a different function, but still in the same class...
    I hope you guys can help, if you need the whole class just say it.
    greetz blackwolf12333

    Ah is this so hard to fix?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  2. Offline

    Norbo11

    Post your whole class, it seems you are assigning the variable wrong or changing it's value somewhere where you shouldn't.
     
  3. ok, here it is:
    Code:
    package me.luuk_the_buunk.commands;
     
    import java.util.HashMap;
     
    import me.luuk_the_buunk.Collour;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
     
    public class ColourCommand implements CommandExecutor, Listener {
     
        public HashMap<String, String> playersWithInstandColor = new HashMap<String, String>();
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
           
            if(cmd.getName().equalsIgnoreCase("c"))
            {
                // /c bluea
                if(args[0].equalsIgnoreCase("bluea"))
                {
                    if(sender instanceof Player)
                    {
                        Collour.log.info("debug1");
                        Player p = (Player) sender;
                        playersWithInstandColor.put(p.getName(), "blue");
                        Collour.log.info(playersWithInstandColor.get("blackwolf12333"));
                        return true;
                    }
                }
               
                // /c blue
                if(args[0].equalsIgnoreCase("blue"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.BLUE + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c red
                if(args[0].equalsIgnoreCase("red"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.RED + args[i] + " ";
                        }
                                   
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c green
                if(args[0].equalsIgnoreCase("green"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.GREEN + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c yellow
                if(args[0].equalsIgnoreCase("yellow"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.YELLOW + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c pink
                if(args[0].equalsIgnoreCase("pink"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.LIGHT_PURPLE + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c aqua
                if(args[0].equalsIgnoreCase("aqua"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.AQUA + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c black
                if(args[0].equalsIgnoreCase("black"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.BLACK + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
                // /c gold
                if(args[0].equalsIgnoreCase("gold"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.GOLD + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c white
                if(args[0].equalsIgnoreCase("white"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.WHITE + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c darkblue
                if(args[0].equalsIgnoreCase("darkblue"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.DARK_BLUE + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
                   
                // /c darkgreen
                if(args[0].equalsIgnoreCase("darkgreen"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.DARK_GREEN + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c darkblue
                if(args[0].equalsIgnoreCase("darkblue"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.DARK_BLUE + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c darkaqua
                if(args[0].equalsIgnoreCase("darkaqua"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.DARK_AQUA + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c darkred
                if(args[0].equalsIgnoreCase("darkred"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                        if(i == 0)
                        {
                          continue;
                        }
                        result += ChatColor.DARK_RED + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
                   
                // /c grey
                if(args[0].equalsIgnoreCase("grey"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.GRAY + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c gray
                if(args[0].equalsIgnoreCase("gray"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.GRAY + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c darkgray
                if(args[0].equalsIgnoreCase("darkgray"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.DARK_GRAY + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
               
                // /c darkgrey
                if(args[0].equalsIgnoreCase("darkgrey"))
                {
                    if(sender instanceof Player)
                    {
                        Player p = (Player) sender;
                        String result = "";
                        for(int i = 0; i < args.length; i++)
                        {
                            if(i == 0)
                            {
                                continue;
                            }
                            result += ChatColor.DARK_GRAY + args[i] + " ";
                        }
                       
                        p.chat(result);
                        return true;
                    }
                }
            }
            return false;
        }
     
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerChat(PlayerChatEvent event)
        {
            Collour.log.info("debug2");
           
            Collour.log.info(event.getPlayer().getName());
           
            Collour.log.info(playersWithInstandColor.get(event.getPlayer().getName()));
           
           
           
            if(playersWithInstandColor.containsKey(event.getPlayer().getName()))
            {
                String color = playersWithInstandColor.get(event.getPlayer().getName());
                String msg = event.getMessage();
                Collour.log.info(color);
               
                String newMessage = Collour.getByName(color) + msg;
                Collour.log.info(newMessage);
               
                event.setMessage(newMessage);
            }
        }
    }
    
    Sorry, it is a bit long:p, but this is it:)
    greetz blackwolf12333
     
  4. Offline

    Malikk

    Perhaps the issue is with the hard coded player name?

    I would change the HashMap to use the actual player class, rather than their string name, less room for error.
     
  5. something whit case incorrect names?
     
  6. Offline

    Malikk

    That's why i suggested he use the Player class, rather than the names
     
  7. using player as keys, would cause it not to persist over reconnects from the player
     
  8. Offline

    Malikk

    If he needs thats, it depends. But at the very least, he should be checking the HashMap with player.getName(), since that's how it put it in.
     
  9. the hardcoded player name was just for testing, and i'll try the Player class
     
  10. Offline

    CorrieKay

    If youre gonna use the Player class as a key, make sure you get a quit event listening, to remove the key whenever they quit, otherwise bukkit is gonna flip its lid (no pun intended) about a persisting player class when it should be garbage collected.

    also, instead of this huge list of if(arg.equals(color)) blocks, you should do this:

    ChatColor.valueOf(arg).name()
     
  11. Offline

    Hidendra

    is "blackwolf12333" your name (exactly, including the case)? If you want case-insensitive matching, you'll probably want to make the player's name you add to the map lower case and then when you .get(), use lowercase as well.
     
  12. Yes, that's not the problem:p

    Hmm, using Player didn't work:(

    The code is not getting further than this line:
    Code:
    if(playersWithInstandColor.containsKey(event.getPlayer().getName())
    that's because when you try to get that key, it returns null instead of "blue" as String.
    greetz blackwolf12333

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  13. your not looking like the kind of people that have 2 instances of playersWithInstandColor variable inside 2 classes, so 2 variables thats called playersWithInstandColor are 2 differend instances, but can you verify that?
     
  14. I don't have that for as far as i know, i don't even know how to do something like that:p

    I just tried to set the playersWithInstandColor in the other function, so the listener function, and that worked, but when i set it in another function and use it in that function it doesn't work:/ weird, anyone know why?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  15. I think you have 2 classes now
    Code:java
    1.  
    2. public class test1
    3. {
    4. Map<String,String> playersWithInstandColor = new HashMap<String,String>();
    5. }
    6. public class test2
    7. {
    8. Map<String,String> playersWithInstandColor = new HashMap<String,String>();
    9. } [syntax]
    10. Your listener is class 1, you main class is class 2, chanced to 1 of them dont reflect the other[/syntax]
     
  16. hmm ok, so how would i fix that?
     
  17. can e get first the code of class oif the listener fuction, and the class where the function that not woring is
     
  18. I already gave it, look at my second reply, that's the source of the listener and the command executor
     
  19. Bump, i would really appreciate it when this gets answered :p
     
  20. Offline

    NuclearW

    blackwolf12333
    If you have two different HashMaps with the same name you need to realize that they don't magically become the same variable.

    Consider storing the HashMap in just one of the classes, and then either making it static and referencing it statically, or saving the plugin instance in the listener class to reference the HashMap that way
     
  21. I can't see that i have the hashmap, which is only one, is in two different classes, because i use the hashmap only in the ColourCommand class. not in any other...
    but ill try what you told me, i am at school atm so i will do that when i am back.
    greetz blackwolf12333
     
Thread Status:
Not open for further replies.

Share This Page