Solved Name cannot be null

Discussion in 'Plugin Development' started by Goksi, Apr 17, 2017.

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

    Goksi

    Hi i get this error when i try to do inventoryclick event
    Error : https://pastebin.com/5swHgfwi

    CommandHandler class code:
    Code:
    package me.goksi.extremebans;
    
    import java.util.Date;
    import java.util.HashMap;
    
    import org.bukkit.BanList;
    import org.bukkit.Bukkit;
    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.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    
    
    public class CommandHandler implements CommandExecutor, Listener {
      
        private Main plugin;
        public CommandHandler(Main pl){
            plugin = pl;
        }
      
        HashMap<String, String> targeth = new HashMap<String, String>();
        String bumper = org.apache.commons.lang.StringUtils.repeat("\n", 35);
      
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if(p.hasPermission("extremebans.gui")){
                    if(args.length == 1){
                        Player target = plugin.getServer().getPlayer(args[0]);
                        if(target != null){
                            p.openInventory(BanGui.gui);
                            targeth.put("target", target.getName());  
                        } else {
                            p.sendMessage(ChatColor.RED + "Igrac nije online!");
                        }
                    } else {
                        p.sendMessage(ChatColor.RED + "Koriscenje komande: /efban <igrac>");
                    }      
            } else {
                p.sendMessage(ChatColor.RED + "You do not have permission!");
            }
            return false;
        }
      
        @EventHandler
        public void onClick(InventoryClickEvent e){
            if(e.getInventory().getTitle().equals(ChatColor.RED + "ExtremeBans")){
                Player target = Bukkit.getServer().getPlayer(targeth.get("target"));
                Player p = (Player) e.getWhoClicked();
                if(e.getSlot() == 10){
                    Bukkit.getBanList(BanList.Type.NAME).addBan(target.getName(), bumper + ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4Illegal client modify(Nisi priznao)" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Bices unbanovan za 12h od vremena bana " + bumper), new Date(System.currentTimeMillis() + 60*60*1000*12), null);
                    target.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4Illegal client modify(Nisi priznao)" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Bices unbanovan za 12h od vremena bana"));
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&8[&cExtreme&fBans&8] &4" + target.getName() + "&7je banovan od strane &4&l" + p.getName() + "&7zbog: &4Illegal client modify"));
                    e.setCancelled(true);
                    p.closeInventory();
                }
                if(e.getSlot() == 12){
                    Bukkit.getBanList(BanList.Type.NAME).addBan(target.getName(), bumper + ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4Illegal client modify(Priznao si)" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Bices unbanovan za 6h od vremena bana " + bumper), new Date(System.currentTimeMillis() + 60*60*1000*6), null);
                    target.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4Illegal client modify(Priznao si)" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Bices unbanovan za 6h od vremena bana "));
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&8[&cExtreme&fBans&8] &4" + target.getName() + "&7je banovan od strane &4&l" + p.getName() + "&7zbog: &4Illegal client modify(Priznao)"));
                    e.setCancelled(true);
                    p.closeInventory();
                }
                if(e.getSlot() == 16){
                    Bukkit.getBanList(BanList.Type.NAME).addBan(target.getName(), bumper + ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4&lReklamiranje" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Ovaj ban traje zauvek " + bumper), null, null);
                    target.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4&lReklamiranje" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Ovaj ban traje zauvek  "));
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&8[&cExtreme&fBans&8] &4" + target.getName() + "&7je banovan od strane &4&l" + p.getName() + "&7zbog: &4&lReklamiranje"));
                    e.setCancelled(true);
                    p.closeInventory();
                }
                if(e.getSlot() == 14){
                    target.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&8Kikovan si od strane: &4" + p.getName() + "\n" + "&8Razlog kicka: &4Fly na ne dozvoljenom mestu"));
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&8[&4Extreme&fBans&8] &4" + p.getName() + "&7je kikovao igraca &4" + target.getName() + "&7."));
                }
            }
        }
    
    }
    Anyone know how to fix this?
    I think error is in HashMap usage
     
  2. Offline

    MrPowWow

    Haven't messed around with HashMaps much but, in the pastebin your provided it says "Caused by: java.lang.IllegalArgumentException: Name cannot be null". So something is null maybe? If that doesn't work try using the Item name instead of the slot.
     
  3. Offline

    Zombie_Striker

    @Goksi
    Are you sure you are registering the same instance of the class? If you create two new instances of this class when setting the executor and registering the listener, then the commands will not be able to interact with the events.
     
  4. Offline

    Mahtaran

    The way you have done the map now is that it contains 1 value:
    "target" - targetname

    This means that it doesn't allow for multiple targets.

    I would say, use UUIDs for the map. In the command, don't use
    Code:
    targeth.put("target", target.getName());
    But rather use
    Code:
    targeth.put(sender.getUniqueID(), target.getUniqueID());
    Then, in the listener, use
    Code:
    Player target = targeth.get(e.getWhoClicked().getUniqueID();
     
  5. Offline

    Goksi

    Hmm i do not understand what are u trying to say ?

    Will try that now
    @Mahtaran still do not work but diffrent error
    Error: https://pastebin.com/NDMN44hG

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Apr 17, 2017
  6. Offline

    yPedx

    @Goksi
    What is line 64 now?
     
  7. Offline

    Mahtaran

    Yeah, that would be useful.
     
  8. Offline

    Caderape2

    @Goksi
    This must be your error. Like zombie_striker said, you are registering a new instance of the class for the command, and one for the event. so they are not linked.

    You can show your onEnable, but its the only error i can see.
     
  9. Offline

    Mahtaran

    Ah yeah, I see
    @Goksi change the targeth map to being static.
     
  10. Offline

    Caderape2

    nope, just create an instance of the commandHandler and pass the same variable to the command and the event
     
  11. Offline

    Mahtaran

    Ah, you mean when registering it?
     
  12. Offline

    Goksi

    @yPedx @Mahtaran Line 64 is
    Code:
    Bukkit.getBanList(BanList.Type.NAME).addBan(target.getName(), bumper + ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4Illegal client modify(Priznao si)" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Bices unbanovan za 6h od vremena bana " + bumper), new Date(System.currentTimeMillis() + 60*60*1000*6), null);
    @Caderape2 What should i actualy do?
    Here is my main:
    Code:
    package me.goksi.extremebans;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
        Main plugin = this;
    
        public void onEnable(){
            getLogger().info("mc.extreme-factions.info");
            getCommand("efban").setExecutor(new CommandHandler(this));
            Bukkit.getServer().getPluginManager().registerEvents(new CommandHandler(this), this);
        }
       
        public void onDisable(){
           
        }
       
    }
    And i made HashMap using uuid in CommandHandler class:
    Code:
    package me.goksi.extremebans;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.BanList;
    import org.bukkit.Bukkit;
    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.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    
    
    public class CommandHandler implements CommandExecutor, Listener {
       
        private Main plugin;
        public CommandHandler(Main pl){
            plugin = pl;
        }
       
        HashMap<UUID, UUID> targeth = new HashMap<UUID, UUID>();
        String bumper = org.apache.commons.lang.StringUtils.repeat("\n", 35);
       
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if(p.hasPermission("extremebans.gui")){
                    if(args.length == 1){
                        Player target = plugin.getServer().getPlayer(args[0]);
                        if(target != null){
                            p.openInventory(BanGui.gui);
                            targeth.put(p.getUniqueId(), target.getUniqueId());   
                        } else {
                            p.sendMessage(ChatColor.RED + "Igrac nije online!");
                        }
                    } else {
                        p.sendMessage(ChatColor.RED + "Koriscenje komande: /efban <igrac>");
                    }       
            } else {
                p.sendMessage(ChatColor.RED + "You do not have permission!");
            }
            return false;
        }
       
        @EventHandler
        public void onClick(InventoryClickEvent e){
            if(e.getInventory().getTitle().equals(ChatColor.RED + "ExtremeBans")){
                Player p = (Player) e.getWhoClicked();
                Player target = Bukkit.getServer().getPlayer(targeth.get(p.getUniqueId()));
                if(e.getSlot() == 10){
                    Bukkit.getBanList(BanList.Type.NAME).addBan(target.getName(), bumper + ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4Illegal client modify(Nisi priznao)" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Bices unbanovan za 12h od vremena bana " + bumper), new Date(System.currentTimeMillis() + 60*60*1000*12), null);
                    target.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4Illegal client modify(Nisi priznao)" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Bices unbanovan za 12h od vremena bana"));
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&8[&cExtreme&fBans&8] &4" + target.getName() + "&7je banovan od strane &4&l" + p.getName() + "&7zbog: &4Illegal client modify"));
                    e.setCancelled(true);
                    p.closeInventory();
                }
                if(e.getSlot() == 12){
                    Bukkit.getBanList(BanList.Type.NAME).addBan(target.getName(), bumper + ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4Illegal client modify(Priznao si)" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Bices unbanovan za 6h od vremena bana " + bumper), new Date(System.currentTimeMillis() + 60*60*1000*6), null);
                    target.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4Illegal client modify(Priznao si)" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Bices unbanovan za 6h od vremena bana "));
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&8[&cExtreme&fBans&8] &4" + target.getName() + "&7je banovan od strane &4&l" + p.getName() + "&7zbog: &4Illegal client modify(Priznao)"));
                    e.setCancelled(true);
                    p.closeInventory();
                }
                if(e.getSlot() == 16){
                    Bukkit.getBanList(BanList.Type.NAME).addBan(target.getName(), bumper + ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4&lReklamiranje" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Ovaj ban traje zauvek " + bumper), null, null);
                    target.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&8Banovan si od strane: &4" + p.getName() + "\n" + "&8Razlog bana: &4&lReklamiranje" + "\n" + "&8Appel website: &4[URL]http://www.extreme-factionsmc.ga/[/URL]" + "\n" + "&8Ovaj ban traje zauvek  "));
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&8[&cExtreme&fBans&8] &4" + target.getName() + "&7je banovan od strane &4&l" + p.getName() + "&7zbog: &4&lReklamiranje"));
                    e.setCancelled(true);
                    p.closeInventory();
                }
                if(e.getSlot() == 14){
                    target.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&8Kikovan si od strane: &4" + p.getName() + "\n" + "&8Razlog kicka: &4Fly na ne dozvoljenom mestu"));
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&8[&4Extreme&fBans&8] &4" + p.getName() + "&7je kikovao igraca &4" + target.getName() + "&7."));
                }
            }
        }
     
  13. Offline

    Mahtaran

    When you do this:
    Code:
    getCommand("efban").setExecutor(new CommandHandler(this));
    Bukkit.getServer().getPluginManager().registerEvents(new CommandHandler(this), this);
    
    You're using 2 instances of CommandHandler.

    Just as a example, you're using 2 pc's. Now, of course these 2 pc's don't have the same data. To fix this, you use the same pc ("instance") when registering. This way, as it's the same pc, you're sure it will contain the same data.
    This was an example!!!

    Now, to use the same instance, you do this:
    Code:
    CommandHandler commandhandler = new CommandHandler(this); //create the "pc"
    getCommand("efban").setExecutor(commandhandler); //register the command on that "pc"
    Bukkit.getServer().getPluginManager().registerEvents(commandhandler, this); //and register the listener with the same "pc"
    
     
  14. Offline

    Goksi

  15. Offline

    Mahtaran

    Nice to know!
     
Thread Status:
Not open for further replies.

Share This Page