Solved NullPointerException

Discussion in 'Plugin Development' started by ItsMas_, Feb 4, 2016.

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

    ItsMas_

    I have a NPE in my code and I cannot figure out what is producing it.

    Error:
    Code:
    [20:38:18 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'p' in plugin Punishments v1.0.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
            at me.mas.punishmenu.BanCommand.onCommand(BanCommand.java:100) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            ... 15 more
    Code: (BanCommand.java class):
    Code:
    package me.mas.punishmenu;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Skull;
    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;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class BanCommand implements CommandExecutor, Listener {
    
        String reason = "";
       
        HashMap<Player, Player> victim = new HashMap<Player, Player>();
        HashMap<Player, String> mapReason = new HashMap<Player, String>();
       
        public static Inventory punishMenu = Bukkit.createInventory(null, 54, ChatColor.RED + "Punish player");
       
        public static void createDisplay(Inventory inv, Material material, int slot, String name, String lore, byte data) {
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            ArrayList<String> Itemlore = new ArrayList<String>();
            Itemlore.add(lore);
            meta.setLore(Itemlore);
            meta.setDisplayName(name);
           
            item.setDurability(data);
            item.setItemMeta(meta);
           
            inv.setItem(slot, item);   
        }
       
        static {
            createDisplay(punishMenu, Material.INK_SACK, 28, "§aSeverity 1", "§bLength: §a4 hours", (byte) 2);
            createDisplay(punishMenu, Material.INK_SACK, 30, "§eSeverity 2", "§bLength: §e1 day", (byte) 11);
            createDisplay(punishMenu, Material.INK_SACK, 32, "§6Severity 3", "§bLength: §67 days", (byte) 14);
            createDisplay(punishMenu, Material.INK_SACK, 34, "§cSeverity 4", "§bLength: §c30 days", (byte) 1);
           
            createDisplay(punishMenu, Material.REDSTONE_BLOCK, 28, "§4§l4Permanent Ban", "§bLength: §4Permanent", (byte) 0);
        }
       
        public static ItemStack getPlayerHead(String name) {
            ItemStack stack = new ItemStack(Material.AIR);
            boolean done = false;
            Location loc = null;
            int i = 0;
            while (!done) {
                if (!Bukkit.getWorld("world").getBlockAt(-29999970,64,-29999970 + i).getType().equals(Material.AIR)) {
                    i++;
                }else{
                    done = Boolean.valueOf(done);
                    done = true;
                    loc = new Location(Bukkit.getWorld("world"),-29999970,64,29999970 + i);
                    break;
                }
            }
            loc.getBlock().setType(Material.SKULL);
            Skull s = (Skull) loc.getBlock().getState();
            s.setOwner(name);
            s.update();
            for (ItemStack s1 : s.getBlock().getDrops()) {
                ItemMeta meta = s1.getItemMeta();
                meta.setDisplayName(ChatColor.YELLOW + name);
                s1.setItemMeta(meta);
                stack = s1;
            }
            s.getBlock().setType(Material.AIR);
            return stack;
        }
       
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            if (cmd.getName().equalsIgnoreCase("p")){
                if (!(sender instanceof Player)){
                    sender.sendMessage("§3Punish §b> §cOnly players can use the punish menu");
                }else{
                    Player p = (Player) sender;
    
                    if (!p.hasPermission("mas.punishmenu.punish")){
                        p.sendMessage("§3Punish §b> §cNo permission");
                    }else{
                        if (args.length <= 1){
                            p.sendMessage("§3Punish §b> §cSyntax: /p <player> <reason>");
                        }else{
                            victim.put(p, Bukkit.getServer().getPlayer(args[0]));
                               
                            if (victim.get(p).hasPermission("mas.punishmenu.exempt")){
                                p.sendMessage("§3Punish §b> §cPlayer §e" + victim.get(p).getName() + " §ccannot be punished");
                               
                            }else{
                           
                                reason = "";
                                for (int b = 1; b < args.length; b++){
                                    reason += args[b] + " ";
                                }
                                reason = reason.trim();
                                mapReason.put(p, reason);
                                reason = "";
    
                                if (victim.get(p).isBanned()){
                                    createDisplay(punishMenu, Material.REDSTONE_TORCH_ON, 28, "§aUnban", "§bClick to unban the player", (byte) 0);
                                    punishMenu.addItem(getPlayerHead(victim.get(p).toString()));
                                }else{
                                    createDisplay(punishMenu, Material.REDSTONE_TORCH_OFF, 28, "§aThis player is not banend", "§bYou cannot unban this player", (byte) 0);
                                    punishMenu.addItem(getPlayerHead(victim.get(p).getName()));
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }
       
        @SuppressWarnings("unused")
        @EventHandler
        public void onClick(InventoryClickEvent e){
            if (e.getInventory().equals(punishMenu)){
                e.setCancelled(true);
               
                Player clicker = (Player) e.getWhoClicked();
                ItemStack clicked = e.getCurrentItem();
               
                if (clicked.getType() == Material.INK_SACK && clicked.getDurability() == 2){
                   
                   
                }
            }
        }
    }
    Any help? Thanks :)
     
  2. Offline

    ShowbizLocket61

    @ItsMas_

    Because the value associated with p is null. getPlayer is returning null, and you're associating that with p.
    Code:
    victim.put(p, Bukkit.getServer().getPlayer(args[0]));
    Now, on this line you get that null value, and try to call hasPermission on it. Nope.
    Code:
    victim.get(p).hasPermission("mas.punishmenu.exempt")
     
  3. Offline

    ItsMas_

    How would I go about fixing this? (Don't post the actual code, just what I need to do)
     
  4. Offline

    I Al Istannen

    @ItsMas_ Save the result of "Bukkit#getPlayer()" in a variable and check if this variable is null. If so, send an error message and return.
     
  5. Offline

    ShowbizLocket61

    @ItsMas_
    Make sure that the target player is online
     
  6. Offline

    ItsMas_

    I am making this for a ban plugin, and I need to be able to ban offline players, so how else could I fix the error but still be able to use the offline player as a target?
     
  7. Offline

    Xerox262

    @ItsMas_ use Bukkit.getOfflinePlayer() instead.
     
  8. Offline

    ItsMas_

    Okay, I have added a null check and changed it to getOfflinePlayer if they are null, getting this error:
    Code:
    [16:35:47 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'p' in plugin Punishments v1.0.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.CraftOfflinePlayer cannot be cast to org.bukkit.permissions.Permissible
            at me.mas.punishmenu.BanCommand.onCommand(BanCommand.java:109) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            ... 15 more
    Updated code:
    Code:
    package me.mas.punishmenu;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.block.Skull;
    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;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.permissions.Permissible;
    
    public class BanCommand implements CommandExecutor, Listener {
    
        ItemStack skull;
      
        String reason = "";
      
        HashMap<Player, Player> victim = new HashMap<Player, Player>();
        HashMap<Player, OfflinePlayer> offlineVictim = new HashMap<Player, OfflinePlayer>();
        HashMap<Player, String> mapReason = new HashMap<Player, String>();
        HashMap<Player, String> victimType = new HashMap<Player, String>();
      
        public static Inventory punishMenu = Bukkit.createInventory(null, 54, ChatColor.RED + "Punish player");
      
        public static void createDisplay(Material material, int slot, String name, String lore, byte data) {
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            ArrayList<String> Itemlore = new ArrayList<String>();
            Itemlore.add(lore);
            meta.setLore(Itemlore);
            meta.setDisplayName(name);
          
            item.setDurability(data);
            item.setItemMeta(meta);
          
            punishMenu.setItem(slot, item);  
        }
      
        static {
            createDisplay(Material.INK_SACK, 28, "§aSeverity 1", "§bLength: §a4 hours", (byte) 2);
            createDisplay(Material.INK_SACK, 30, "§eSeverity 2", "§bLength: §e1 day", (byte) 11);
            createDisplay(Material.INK_SACK, 32, "§6Severity 3", "§bLength: §67 days", (byte) 14);
            createDisplay(Material.INK_SACK, 34, "§cSeverity 4", "§bLength: §c30 days", (byte) 1);
          
            createDisplay(Material.REDSTONE_BLOCK, 28, "§4§l4Permanent Ban", "§bLength: §4Permanent", (byte) 0);
        }
      
        public static ItemStack getPlayerHead(String name) {
            ItemStack stack = new ItemStack(Material.AIR);
            boolean done = false;
            Location loc = null;
            int i = 0;
            while (!done) {
                if (!Bukkit.getWorld("world").getBlockAt(-29999970,64,-29999970 + i).getType().equals(Material.AIR)) {
                    i++;
                }else{
                    done = Boolean.valueOf(done);
                    done = true;
                    loc = new Location(Bukkit.getWorld("world"),-29999970,64,29999970 + i);
                    break;
                }
            }
            loc.getBlock().setType(Material.SKULL);
            Skull s = (Skull) loc.getBlock().getState();
            s.setOwner(name);
            s.update();
            for (ItemStack s1 : s.getBlock().getDrops()) {
                ItemMeta meta = s1.getItemMeta();
                meta.setDisplayName(ChatColor.YELLOW + name);
                s1.setItemMeta(meta);
                stack = s1;
            }
            s.getBlock().setType(Material.AIR);
            return stack;
        }
      
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
          
            if (cmd.getName().equalsIgnoreCase("p")){
                if (!(sender instanceof Player)){
                    sender.sendMessage("§3Punish §b> §cOnly players can use the punish menu");
                }else{
                    Player p = (Player) sender;
    
                    if (!p.hasPermission("mas.punishmenu.punish")){
                        p.sendMessage("§3Punish §b> §cNo permission");
                    }else{
                        if (args.length <= 1){
                            p.sendMessage("§3Punish §b> §cSyntax: /p <player> <reason>");
                        }else{
    
                            if (Bukkit.getPlayer(args[0]) == null){
                                offlineVictim.put(p, Bukkit.getOfflinePlayer(args[0]));
                                victimType.put(p, "offline");
                              
                                if (((Permissible) offlineVictim.get(p)).hasPermission("mas.punishmenu.exempt")){
                                    p.sendMessage("§3Punish §b> §cPlayer §e" + offlineVictim.get(p).getName() + " §ccannot be punished");
                                  
                                }else{
                              
                                    reason = "";
                                    for (int b = 1; b < args.length; b++){
                                        reason += args[b] + " ";
                                    }
                                    reason = reason.trim();
                                    mapReason.put(p, reason);
                                    reason = "";
      
                                    if (offlineVictim.get(p).isBanned()){
                                        skull = getPlayerHead(victim.get(p).getName());
                                        punishMenu.setItem(13, skull);
                                        createDisplay(Material.REDSTONE_TORCH_ON, 28, "§aUnban", "§bClick to unban the player", (byte) 0);
                                        p.openInventory(punishMenu);
                                    }else{
                                        skull = getPlayerHead(victim.get(p).getName());
                                        punishMenu.setItem(13, skull);
                                        createDisplay(Material.REDSTONE_TORCH_OFF, 28, "§aThis player is not banend", "§bYou cannot unban this player", (byte) 0);
                                        p.openInventory(punishMenu);
                                    }
                                }
                              
                            }else{
                                  
                                victim.put(p, Bukkit.getPlayer(args[0]));
                                victimType.put(p, "online");
                              
                                if (victim.get(p).hasPermission("mas.punishmenu.exempt")){
                                    p.sendMessage("§3Punish §b> §cPlayer §e" + victim.get(p).getName() + " §ccannot be punished");
                                  
                                }else{
                              
                                    reason = "";
                                    for (int b = 1; b < args.length; b++){
                                        reason += args[b] + " ";
                                    }
                                    reason = reason.trim();
                                    mapReason.put(p, reason);
                                    reason = "";
      
                                    if (victim.get(p).isBanned()){
                                        skull = getPlayerHead(victim.get(p).getName());
                                        punishMenu.setItem(13, skull);
                                        createDisplay(Material.REDSTONE_TORCH_ON, 28, "§aUnban", "§bClick to unban the player", (byte) 0);
                                        p.openInventory(punishMenu);
                                    }else{
                                        createDisplay(Material.REDSTONE_TORCH_OFF, 28, "§aThis player is not banend", "§bYou cannot unban this player", (byte) 0);
                                        skull = getPlayerHead(victim.get(p).getName());
                                        punishMenu.setItem(13, skull);
                                        p.openInventory(punishMenu);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }
      
        @SuppressWarnings("unused")
        @EventHandler
        public void onClick(InventoryClickEvent e){
            if (e.getInventory().equals(punishMenu)){
                e.setCancelled(true);
              
                Player clicker = (Player) e.getWhoClicked();
                ItemStack clicked = e.getCurrentItem();
              
                if (clicked.getType() == Material.INK_SACK && clicked.getDurability() == 2){
                  
                  
                }
            }
        }
    }
     
  9. Offline

    ShowbizLocket61

    @ItsMas_
    I don't think CraftOfflinePlayer implements the Permissible interface.
    Call getPlayer on the CraftOfflinePlayer, like this:
    Code:
    if(((Permissible) offlineVictim.get(p).getPlayer()).hasPermission("mas.punishmenu.exempt")){
     
  10. Offline

    ItsMas_

    That gives me the same error, but with a NullPointerException:
    Code:
    [17:08:21 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'p' in plugin Punishments v1.0.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
            at me.mas.punishmenu.BanCommand.onCommand(BanCommand.java:109) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            ... 15 more
     
  11. Offline

    Xerox262

    @ItsMas_ OfflinePlayer#getPlayer(); returns null if the player is offline, you can't get the perms of an offline player with the Bukkit API. However, if you have a plugin managing permissions you should look into their API and documentation, they might support it, if you use Vault and a Vault compatible permission manager look at the Vault API too. Also post what permission manager you're using so we can try help find it too, unless you're using a custom one.

    If you can't find anything then you might have to skip the exempt check for an offline player, you can check if they're online though and if they are then check their perms with OfflinePlayer#isOnline(); then OfflinePlayer#getPlayer(); then Player#hasPermission();

    Edit: Vault does in fact have a way to look up an offline player http://milkbowl.github.io/VaultAPI/..., org.bukkit.OfflinePlayer, java.lang.String)
     
    Last edited: Feb 5, 2016
  12. Offline

    ItsMas_

    I have changed it to use (offlinePlayer.isop() instead, will edit this post with the result

    EDIT:
    Current code:
    Code:
    package me.mas.punishmenu;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.block.Skull;
    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;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class BanCommand implements CommandExecutor, Listener {
    
        ItemStack skull;
       
        String reason = "";
       
        HashMap<Player, Player> victim = new HashMap<Player, Player>();
        HashMap<Player, OfflinePlayer> offlineVictim = new HashMap<Player, OfflinePlayer>();
        HashMap<Player, String> mapReason = new HashMap<Player, String>();
        HashMap<Player, String> victimType = new HashMap<Player, String>();
       
        public static Inventory punishMenu = Bukkit.createInventory(null, 54, ChatColor.RED + "Punish player");
       
        public static void createDisplay(Material material, int slot, String name, String lore, byte data) {
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            ArrayList<String> Itemlore = new ArrayList<String>();
            Itemlore.add(lore);
            meta.setLore(Itemlore);
            meta.setDisplayName(name);
           
            item.setDurability(data);
            item.setItemMeta(meta);
           
            punishMenu.setItem(slot, item);   
        }
       
        static {
            createDisplay(Material.INK_SACK, 28, "§aSeverity 1", "§bLength: §a4 hours", (byte) 2);
            createDisplay(Material.INK_SACK, 30, "§eSeverity 2", "§bLength: §e1 day", (byte) 11);
            createDisplay(Material.INK_SACK, 32, "§6Severity 3", "§bLength: §67 days", (byte) 14);
            createDisplay(Material.INK_SACK, 34, "§cSeverity 4", "§bLength: §c30 days", (byte) 1);
           
            createDisplay(Material.REDSTONE_BLOCK, 49, "§4§lPermanent Ban", "§bLength: §4Permanent", (byte) 0);
        }
       
        public static ItemStack getPlayerHead(String name) {
            ItemStack stack = new ItemStack(Material.AIR);
            boolean done = false;
            Location loc = null;
            int i = 0;
            while (!done) {
                if (!Bukkit.getWorld("world").getBlockAt(-29999970,64,-29999970 + i).getType().equals(Material.AIR)) {
                    i++;
                }else{
                    done = Boolean.valueOf(done);
                    done = true;
                    loc = new Location(Bukkit.getWorld("world"),-29999970,64,29999970 + i);
                    break;
                }
            }
            loc.getBlock().setType(Material.SKULL);
            Skull s = (Skull) loc.getBlock().getState();
            s.setOwner(name);
            s.update();
            for (ItemStack s1 : s.getBlock().getDrops()) {
                ItemMeta meta = s1.getItemMeta();
                meta.setDisplayName(ChatColor.YELLOW + name);
                s1.setItemMeta(meta);
                stack = s1;
            }
            s.getBlock().setType(Material.AIR);
            return stack;
        }
       
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            if (cmd.getName().equalsIgnoreCase("p")){
                if (!(sender instanceof Player)){
                    sender.sendMessage("§3Punish §b> §cOnly players can use the punish menu");
                }else{
                    Player p = (Player) sender;
    
                    if (!p.hasPermission("mas.punishmenu.punish")){
                        p.sendMessage("§3Punish §b> §cNo permission");
                    }else{
                        if (args.length <= 1){
                            p.sendMessage("§3Punish §b> §cSyntax: /p <player> <reason>");
                        }else{
    
                            if (Bukkit.getPlayer(args[0]) == null){
                                offlineVictim.put(p, Bukkit.getOfflinePlayer(args[0]));
                                victimType.put(p, "offline");
                               
                                if (offlineVictim.get(p).isOp()){
                                    p.sendMessage("§3Punish §b> §cPlayer §e" + offlineVictim.get(p).getName() + " §ccannot be punished");
                                   
                                }else{
                                   
                                    if (!offlineVictim.get(p).hasPlayedBefore()){
                                        p.sendMessage("§3Punish §b> §cPlayer §e" + offlineVictim.get(p).getName() + "§c has never joined before");
                                        return true;
                                    }else{
                                   
                                        reason = "";
                                        for (int b = 1; b < args.length; b++){
                                            reason += args[b] + " ";
                                        }
                                        reason = reason.trim();
                                        mapReason.put(p, reason);
                                        reason = "";
           
                                        if (offlineVictim.get(p).isBanned()){
                                            skull = getPlayerHead(victim.get(p).getName());
                                            punishMenu.setItem(13, skull);
                                            createDisplay(Material.REDSTONE_TORCH_ON, 28, "§aUnban", "§bClick to unban the player", (byte) 0);
                                            p.openInventory(punishMenu);
                                        }else{
                                            skull = getPlayerHead(victim.get(p).getName());
                                            punishMenu.setItem(13, skull);
                                            createDisplay(Material.REDSTONE_TORCH_OFF, 28, "§aThis player is not banned", "§bYou cannot unban this player", (byte) 0);
                                            p.openInventory(punishMenu);
                                        }
                                    }
                                }
                               
                            }else{
                                   
                                victim.put(p, Bukkit.getPlayer(args[0]));
                                victimType.put(p, "online");
                               
                                if (victim.get(p).isOp()){
                                    p.sendMessage("§3Punish §b> §cPlayer §e" + victim.get(p).getName() + " §ccannot be punished");
                                   
                                }else{
                               
                                    reason = "";
                                    for (int b = 1; b < args.length; b++){
                                        reason += args[b] + " ";
                                    }
                                    reason = reason.trim();
                                    mapReason.put(p, reason);
                                    reason = "";
       
                                    if (victim.get(p).isBanned()){
                                        skull = getPlayerHead(victim.get(p).getName());
                                        punishMenu.setItem(13, skull);
                                        createDisplay(Material.REDSTONE_TORCH_ON, 18, "§aUnban", "§bClick to unban the player", (byte) 0);
                                        p.openInventory(punishMenu);
                                    }else{
                                        createDisplay(Material.REDSTONE_TORCH_OFF, 18, "§aThis player is not banend", "§bYou cannot unban this player", (byte) 0);
                                        skull = getPlayerHead(victim.get(p).getName());
                                        punishMenu.setItem(13, skull);
                                        p.openInventory(punishMenu);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }
       
        @SuppressWarnings("unused")
        @EventHandler
        public void onClick(InventoryClickEvent e){
            if (e.getInventory().equals(punishMenu)){
                e.setCancelled(true);
               
                Player clicker = (Player) e.getWhoClicked();
                ItemStack clicked = e.getCurrentItem();
               
                if (clicked.getType() == Material.INK_SACK && clicked.getDurability() == 2){
                   
                   
                }
            }
        }
    }
    Error:
    Code:
    [09:44:33 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'p' in plugin Punishments v1.0.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
            at me.mas.punishmenu.BanCommand.onCommand(BanCommand.java:132) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            ... 15 more
    This error only appears when trying to use /p <offlineplayer> reason
     
    Last edited: Feb 6, 2016
  13. Offline

    Xerox262

    Read line 132 and try find the null.
     
  14. Offline

    ItsMas_

    Thanks, I solved it :)
     
Thread Status:
Not open for further replies.

Share This Page