Solved Error when calling method

Discussion in 'Plugin Development' started by cheeseypotato, Nov 3, 2019.

  1. Offline

    cheeseypotato

    Yo, I'm trying to call a method from another class using a command, and whenever i try it throws this back at me and i'm unsure why. Any help?

    Code:
    [15:49:52] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'givewand' in plugin MagicalPoodle v1.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) ~[server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481) ~[server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:887) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [server.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at me.billycombo.Main.items.WandCreation.giveWand(WandCreation.java:32) ~[?:?]
            at me.billycombo.Main.commands.GiveWand.onCommand(GiveWand.java:38) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[server.jar:git-Spigot-9de398a-9c887d4]
            ... 17 more
    
    Class Causing Error:
    Code:
    package me.billycombo.Main.commands;
    
    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.Listener;
    
    import me.billycombo.Main.Main;
    import me.billycombo.Main.items.WandCreation;
    
    public class GiveWand implements CommandExecutor, Listener {
       
        public String cmd1 = "givewand";
       
        private Main plugin;
       
        public GiveWand(Main plugin) {
            this.plugin = plugin;
        }
       
        public WandCreation wandCreation = new WandCreation(plugin);
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase(cmd1)) {
                if (args.length != 1) {
                    sender.sendMessage(ChatColor.RED + "Invalid usage, please try again using this format - /givebook [Player]");
                    return true;
                }else {
                       
                    if (Bukkit.getPlayer(args[0]) != null) {
                       
                        Player p = Bukkit.getPlayer(args[0]);
                       
                        wandCreation.giveWand(p);
                       
                        p.sendMessage(ChatColor.GREEN + "Wand given!");
                       
                    } else {
                        sender.sendMessage(ChatColor.RED + "Invalid player name!");
                        return true;
                    }
                   
                    return true;
                }}
            return true;}
    }
    Class That Contains Method
    Code:
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.billycombo.Main.Main;
       
        public class WandCreation {
       
            private Main plugin;
           
            public WandCreation(Main plugin) {
                this.plugin = plugin;
            }
           
        public void giveWand(Player p) {
           
            //Set<String> keys = plugin.getConfig().getConfigurationSection("WandTypes.Wood").getKeys(false);
            //int result = new Random().nextInt(keys.size());
            //String key = (String) keys.toArray()[result];
           
            p.sendMessage(plugin.getConfig().getString("Lumos"));
           
            ItemStack book = new ItemStack(Material.STICK, 1);
            book.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
            ItemMeta bookMeta = book.getItemMeta();
            bookMeta.setDisplayName("Magic Wand");
            bookMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            List<String> bookLore = new ArrayList<String>();
            bookLore.add(ChatColor.DARK_PURPLE + "Owner: " + ChatColor.GOLD +  p.getName());
            bookLore.add("");
            //bookLore.add(ChatColor.DARK_PURPLE + "Wood: " + ChatColor.GOLD + key);
            bookLore.add(ChatColor.DARK_PURPLE + "Wand Core: " + ChatColor.GOLD + "core");
            bookLore.add(ChatColor.DARK_PURPLE + "Length: " + ChatColor.GOLD + "length");
            bookLore.add(ChatColor.DARK_PURPLE + "Flexibility: " + ChatColor.GOLD + "flex");
            bookMeta.setLore(bookLore);
            book.setItemMeta(bookMeta);
           
            p.getInventory().addItem(book);
            p.updateInventory();
            return;
        }
       
       
    }
     
    Last edited by a moderator: Nov 3, 2019
  2. Online

    timtower Moderator Moderator

    @cheeseypotato
    public WandCreation wandCreation = new WandCreation(plugin);
    That initialization needs to happen in the constructor, not outside of it.
     
    cheeseypotato likes this.
  3. Offline

    cheeseypotato

Share This Page