Stuck on an issue...

Discussion in 'Spigot Plugin Development' started by joshverd, Jun 9, 2016.

  1. Hello spigot community! I am a new developer and I have an issue. Every time I try and run this plugin (not the full plugin)

    Code (Text):
        public boolean onCommand(CommandSender sendr, Command cmd, String alias, String[] args) {
            if(sendr instanceof Player) {
               
                   
               
               
           
                if(cmd.getName().equalsIgnoreCase("test")){
                    Player player = (Player) sendr;
                    String players = (player.getName());
                    //player.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "The Block" + ChatColor.BOLD + "" + ChatColor.AQUA + ">>>" + ChatColor.GOLD + " Opening TheBlock MOD menu!" );
                        player.openInventory(inv);
                   


                }
            return true;
            }
            return true;}}


    I get an "
    [02:11:32 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin" With the source at the code above. I have tried many things, and none have worked! Sorry if this is a small mistake, as i'm still learning :p
     
  2. MiniDigger

    Supporter

    post the entire stacktrace.
    also, try formatting your code, in the current state it is not really readable.
     
  3. As said by @MiniDigger please post your full stacktrace. However from the code posted I'd imagine it's "inv" which is null as you've not set that anywhere in the code given.
     
  4. [02:44:01 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin TB_inv v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-8a048fe-d8637df]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-8a048fe-d8637df]
    at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1349) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1184) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_66]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
    at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-8a048fe-d8637df]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.NullPointerException
    at org.bukkit.craftbukkit.v1_9_R2.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:183) ~[spigot.jar:git-Spigot-8a048fe-d8637df]
    at test.cmd.onCommand(cmd.java:146) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-8a048fe-d8637df]
    ... 15 more

    Code (Text):
    package test;

    import java.util.List;
    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.InventoryHolder;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import test.Main;

    public class cmd implements Listener, CommandExecutor {

        List<String> toggledFly = new ArrayList<String>();

        private Inventory inv;
        private ItemStack v, ely, inspector, rbI, tps;

        public void Menu() {
            InventoryHolder ss = null;
            inv = Bukkit.getServer().createInventory(ss, 18, "TheBlock Mod Menu!");

            ely = createItem(Material.ELYTRA, 1, 0, ChatColor.GREEN + "Fly Mode");

            v = createItem(Material.GLASS, 1, 0, ChatColor.GREEN + "Vanish");
            inspector = createItem(Material.TRIPWIRE_HOOK, 1, 0, ChatColor.GREEN + "Inspect Tool");
            rbI = createItem(Material.TNT, 4, 4, ChatColor.GREEN + "Quick Explosion Rollback");
            tps = createItem(Material.REDSTONE_BLOCK, 4, 4, ChatColor.GREEN + "Ticks Per Second (TPS)");

            inv.setItem(0, ely);
            inv.setItem(1, v);
            inv.setItem(2, inspector);
            inv.setItem(3, rbI);
            inv.setItem(4, tps);

        }

        private ItemStack createItem(Material m, int j, int k, String name) {
            ItemStack i = new ItemStack(m);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);

            i.setItemMeta(im);
            return i;
        }

        public void show(Player p) {
            p.openInventory(inv);
        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {

            if (!e.getInventory().getName().equalsIgnoreCase(inv.getName()))
                return;
            if (e.getCurrentItem().getItemMeta() == null)
                return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Fly Mode")) {
                e.setCancelled(true);
                e.getWhoClicked();
                Player playerr = (Player) e.getWhoClicked();
                String player = playerr.getName();
                if (toggledFly.contains(playerr.getName())) {
                    playerr.sendMessage("fly turned off!");
                    toggledFly.remove(playerr.getName());
                    playerr.setAllowFlight(false);
                    playerr.setFlying(false);

                } else {
                    playerr.sendMessage("fly turned on!");
                    toggledFly.add(playerr.getName());
                    playerr.setAllowFlight(true);
                    playerr.setFlying(true);
                }
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Vanish")) {
                e.setCancelled(true);
                Player playerr = (Player) e.getWhoClicked();
                String player = playerr.getName();
                Bukkit.dispatchCommand(playerr, "v");
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Inspect Tool")) {
                e.setCancelled(true);

                Player playerr = (Player) e.getWhoClicked();
                String player = playerr.getName();
                Bukkit.dispatchCommand(playerr, "co i");
            }

            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Quick Explosion Rollback")) {
                e.setCancelled(true);

                Player playerr = (Player) e.getWhoClicked();
                String player = playerr.getName();
                Bukkit.dispatchCommand(playerr, "co rollback r:10 t:100h u:#creeper");
                Bukkit.dispatchCommand(playerr, "co rollback r:10 t:100h u:#tnt");
                playerr.sendMessage("Rolled back explosion damage!");
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Ticks Per Second (TPS)")) {
                e.setCancelled(true);

                Player playerr = (Player) e.getWhoClicked();
                String player = playerr.getName();
                Bukkit.dispatchCommand(playerr, "tps");
                e.getWhoClicked().closeInventory();
            }
        }

        public boolean onCommand(CommandSender sendr, Command cmd, String alias, String[] args) {
            if (sendr instanceof Player) {

                if (cmd.getName().equalsIgnoreCase("test")) {
                    Player player = (Player) sendr;
                    // String players = (player.getName());
                    // player.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "The
                    // Block" + ChatColor.BOLD + "" + ChatColor.AQUA + ">>>" +
                    // ChatColor.GOLD + " Opening TheBlock MOD menu!" );
                    player.openInventory(inv);

                }
            }
            return true;
        }
    }
     
     
  5. Pretty sure you're not calling the "Menu" method which means your "inv" variable is null.
     
  6. I got it! Thanks so much!