Solved null console error

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, May 14, 2016.

  1. So, I've been stuck on this for an hour or so. Basically when the player types /xporb <amount>, it says 'An internal error occurred while attempting to perform this command' in the chat, and I also get this big error in the console.
    Code (Text):
    null
    org.bukkit.command.CommandException: Unhandled exception executing command 'xporb' in plugin MainPlugin v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1350) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1185) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_92]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_92]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.MaxKrissigo.MainPlugin.Main.onCommand(Main.java:302) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        ... 15 more
     
    Here is my Main class:
    Code (Text):
    package me.MaxKrissigo.MainPlugin;

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;

    public class Main extends JavaPlugin implements Listener {
     
        private Menu menu;
        private Cosmetic cosmetic;
        private Kit kit;
        private Enchant enchant;
     
        HashMap<String, Scoreboard> playerSb = new HashMap<String,Scoreboard>();
     
        @Override
        public void onEnable() {
         
                menu = new Menu(this);
                kit = new Kit(this);
                enchant = new Enchant(this);
                cosmetic = new Cosmetic(this);
             
                Bukkit.getServer().getPluginManager().registerEvents(this, this);
             
                if (!Bukkit.getPluginManager().isPluginEnabled("HolographicDisplays")) {
                    getLogger().severe("*** HolographicDisplays is not installed or not enabled. ***");
                    getLogger().severe("*** This plugin will be disabled. ***");
                    this.setEnabled(false);
                    return;
                }
             
                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                 
                    public void run() {
                     
                         for (Entity en : Bukkit.getWorld("world").getEntities()) {
                                if(!(en instanceof Player)) {
                                 
                                    en.remove();
                                }
                             }
                 
                     for (Entity en : Bukkit.getWorld("world_nether").getEntities()) {
                            if(!(en instanceof Player)) {
                             
                                en.remove();
                            }
                         }
             
                 for (Entity en : Bukkit.getWorld("world_the_end").getEntities()) {
                        if(!(en instanceof Player)) {
                         
                            en.remove();
                        }
                     }
               
                 Bukkit.broadcastMessage("All entities have been removed!");
         
            }
                     
                    }, 1 * 20, 60 * 20);
             
            }    
             
             
        public void scoreBoardUpdate(Player p) {
         
            String uuid = p.getUniqueId().toString();
         
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getNewScoreboard();
            Objective objective = board.registerNewObjective("health", "dummy");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(ChatColor.BOLD + "" + ChatColor.GOLD + "Kit " + "PVP" + ChatColor.RED + " [BETA]");
         
            int kills = getConfig().getInt("Players." + uuid + ".Kills");
            int deaths = getConfig().getInt("Players." + uuid + ".Deaths");
            Double kdrd = (double) (kills/deaths);
         
            List<Score> scores = Arrays.asList(
                    objective.getScore(""),
                    objective.getScore(""),
                    objective.getScore("Kills: " + (int)kills),
                    objective.getScore("Deaths: " + (int)deaths),
                    objective.getScore("KDR: " + (double)kdrd),
                    objective.getScore(""),
                    objective.getScore("Best Kill Streak: "),
                    objective.getScore("Current Kill Streak: "),
                    objective.getScore(""),
                    objective.getScore("XP: " + p.getExp()));
                 
             
         
            int order = scores.size();
            for(Score s : scores){
                s.setScore(order);
                order--;
            }
         
            p.setScoreboard(board);
         
            playerSb.put(p.getName(), board);
         
        }
     
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent e) {
         
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
             
                public void run() {
                 
                    Player p = e.getPlayer();
                    String uuid = p.getUniqueId().toString();
                 
                    ScoreboardManager manager = Bukkit.getScoreboardManager();
                    Scoreboard board = manager.getNewScoreboard();
                    Objective objective = board.registerNewObjective("health", "dummy");
                    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                    objective.setDisplayName(ChatColor.BOLD + "" + ChatColor.GOLD + "Kit " + "PVP" + ChatColor.RED + " [BETA]");
                 
                    int kills = getConfig().getInt("Players." + uuid + ".Kills");
                    int deaths = getConfig().getInt("Players." + uuid + ".Deaths");
                    Double kdrd = (double) (kills/deaths);
                 
                    List<Score> scores = Arrays.asList(
                            objective.getScore(""),
                            objective.getScore(""),
                            objective.getScore("Kills: " + (int)kills),
                            objective.getScore("Deaths: " + (int)deaths),
                            objective.getScore("KDR: " + (double)kdrd),
                            objective.getScore(""),
                            objective.getScore("Best Kill Streak: "),
                            objective.getScore("Current Kill Streak: "),
                            objective.getScore(""),
                            objective.getScore("XP: " + p.getExp()));
                         
                     
                 
                    int order = scores.size();
                    for(Score s : scores){
                        s.setScore(order);
                        order--;
                    }
                 
                    p.setScoreboard(board);
                 
                    playerSb.put(p.getName(), board);
                 
                 
                }
                 
                }, 1, 1);
         
            Player p = e.getPlayer();
            String uuid = p.getUniqueId().toString();
         
            if (!getConfig().contains("Players." + uuid)) {
                getConfig().set("Players." + uuid + ".Kills", 0);
                getConfig().set("Players." + uuid + ".Deaths", 0);        
                saveConfig();
            }
        }
     
        @EventHandler
        public void onPlayerRespawnEvent(PlayerRespawnEvent e) {
         
            Player p = e.getPlayer();
         
        }
     
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            Player p = e.getEntity();    
         
         
            e.getDrops().clear();
            e.setDroppedExp(0);
            if(p.getKiller() instanceof Player) {
             
                String player = e.getEntity().getName();
                String killer = p.getKiller().getName();
             
                Player k = p.getKiller();
                String pUUID = p.getUniqueId().toString();
                String kUUID = k.getUniqueId().toString();
                int kills = getConfig().getInt("Players." + kUUID + ".Kills");
                int deaths = getConfig().getInt("Players." + pUUID + ".Deaths");
             
                getConfig().set("Players." + kUUID + ".Kills", kills +1);
                getConfig().set("Players." + pUUID + ".Deaths", deaths +1);
                saveConfig();
             
                k.giveExp(6);
             
                String uuid = p.getUniqueId().toString();
             
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();
                Objective objective = board.registerNewObjective("health", "dummy");
                objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                objective.setDisplayName(ChatColor.BOLD + "" + ChatColor.GOLD + "Kit " + "PVP" + ChatColor.RED + " [BETA]");
             
                Double kdrd = (double) (kills/deaths);
             
                List<Score> scores = Arrays.asList(
                        objective.getScore(""),
                        objective.getScore(""),
                        objective.getScore("Kills: " + (int)kills),
                        objective.getScore("Deaths: " + (int)deaths),
                        objective.getScore("KDR: " + (double)kdrd),
                        objective.getScore(""),
                        objective.getScore("Best Kill Streak: "),
                        objective.getScore("Current Kill Streak: "),
                        objective.getScore(""),
                        objective.getScore("XP: " + p.getExp()));
                     
                 
             
                int order = scores.size();
                for(Score s : scores){
                    s.setScore(order);
                    order--;
                }
             
                p.setScoreboard(board);
                k.setScoreboard(board);
             
                playerSb.put(p.getName(), board);
                playerSb.put(k.getName(), board);
             
             
            }
         
        }
     
        public String color(String msg) {
            return ChatColor.translateAlternateColorCodes('&', msg);
         
        }
     
         public boolean onCommand(CommandSender sender, Command command,
                    String commandLabel, String[] args) {
                if (command.getName().equalsIgnoreCase("kit") && sender.hasPermission("mainplugin.kit")) {
                 
                    Player p = (Player) sender;
                 
                    kit.show(p.getPlayer());
                 
                }
                if (command.getName().equalsIgnoreCase("enchants") && sender.hasPermission("mainplugin.enchants")) {
                 
                    Player p = (Player) sender;
                 
                    enchant.show(p.getPlayer());
                 
                }
                if (command.getName().equalsIgnoreCase("cosmetic") && sender.hasPermission("mainplugin.cosmetic")) {
                 
                    Player p = (Player) sender;
                 
                    cosmetic.show(p.getPlayer());
                 
                }
                if (command.getName().equalsIgnoreCase("xporb") && sender.hasPermission("mainplugin.xporb.create")) {
                 
                    Player p = (Player) sender;
                    PlayerInventory inventory = p.getInventory();

                 
                    if (args.length == 0) {
                     
                        p.sendMessage(ChatColor.RED + "You must specify an amoung of xp!");
                     
                    } else if (args.length == 1) {
                     
                        if(isInt(args[1])) {
                         
                     
                        int xp = Integer.parseInt(args[1]);
                         
                            p.setTotalExperience(p.getTotalExperience() - xp);

                            p.sendMessage("fff");
                            inventory.addItem(new ItemStack(Material.SLIME_BALL, 1));

                        }
                         

                         
                         
                        } else {
                         
                            p.sendMessage(ChatColor.RED + "You must specify the amount of xp you want to withdraw!");
                         
                        }
                }
                return true;
         }
       
         public boolean isInt(String s) {
                try {
                    Integer.parseInt(s);
                    return true;
                } catch (NumberFormatException e) {
                    return false;
                }
            }
           
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
         
            Player player = e.getPlayer();
            Action action = e.getAction();
                 
             if ((action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) && player.getItemInHand().getType() == Material.COMPASS) {
                menu.show(e.getPlayer());
                }
        }
     
    }
     
     
    #1 MaxKrissigo, May 14, 2016
    Last edited: May 14, 2016
  2. Your problem is located here:

    Code (Text):

    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.MaxKrissigo.MainPlugin.Main.onCommand(Main.java:302) ~[?:?]
     
     
  3. Thank you pointing that out, although I don't see anything wrong with that line. As in it seems all functional.
     
  4. You have an incorrect argument check. You are checking for argument-one, which in this case is not there nor does it exist. You would want to check for argument-zero, the first argument. An example of this scenario would be this.

    Code (Text):

    /command <arg0> <arg1>
     
    Currently, you are parsing it as

    Code (Text):

    /command <arg1>
     
    Ultimately, you should check if argument-zero is an integer and get the integer information from argument-zero. The reason this is so, is because starting index of arrays is always zero. Indexes are the distance of the array elements from the initial element. Thus, the first element is index[0], which is zero positions away from itself. Next is index[1] which is one unit away from initial element and so on, next is index[2] which is two units away from initial element.
     
    • Agree Agree x 1
  5. Thank you so much! I'm also really dumb for letting that slip passed me.