Solved Error: Item cannot be null

Discussion in 'Plugin Development' started by Akilogramofstone, Nov 10, 2021.

Thread Status:
Not open for further replies.
  1. Hey, so I'm a beginner at this so this might be dumb, but I was making a custom command that gives a custom item and then I saw this error in my server logs. Any ideas how to fix it?

    Error:
    Code:
    [20:19:06 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'magicwand' in plugin MegaPlugin v0.1.3
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
            at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.base/java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: java.lang.IllegalArgumentException: Item cannot be null
            at org.apache.commons.lang.Validate.noNullElements(Validate.java:364) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventory.addItem(CraftInventory.java:265) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at me.deluck1est.megaplugin.commands.WandCommand.onCommand(WandCommand.java:19) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Akilogramofstone
    Caused by: java.lang.IllegalArgumentException: Item cannot be null
    Seems pretty clear to me.
     
  3. Offline

    pixelrider2000

    Stack traces usually tell you everything you need to know. Do me a favor and learn reading them. And without knowing your code we can't help you anyways just by looking at the error.
     
  4. So, I'm guessing the error is caused by class "WandCommand" at line 19?

    Code:
    package me.deluck1est.megaplugin.commands;
    
    import me.deluck1est.megaplugin.CustomItems;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class WandCommand implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("magicwand")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
    
                    if (sender.isOp()) {
                        player.getInventory().setItemInHand(CustomItems.wand);
                        player.sendMessage(ChatColor.GOLD + "Successfully gave " + ChatColor.GRAY + player.getDisplayName() + ChatColor.GOLD + " " + CustomItems.wand.getAmount() + ChatColor.AQUA + " Magic Wand" + ChatColor.GOLD + ".");
                    }
                }
            }
    
            return true;
        }
    
    }
     
  5. Offline

    timtower Administrator Administrator Moderator

  6. Offline

    Tim_M

    Check if customitem.wand is null and it is that's the reason you're getting an error.
     
  7. I don't think it is, though.
    Code:
    package me.deluck1est.megaplugin;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class CustomItems {
    
        public static ItemStack wand;
    
        public static void init() {
            createWand();
        }
    
        private static void createWand() {
            ItemStack item = new ItemStack(Material.STICK, 1);
            ItemMeta meta = item.getItemMeta();
            List<String> lore = new ArrayList<>();
    
            meta.setDisplayName(ChatColor.AQUA + "Magic Wand");
            lore.add(ChatColor.ITALIC + "" + ChatColor.GRAY + "launch");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.KNOCKBACK, 1, false);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            item.setItemMeta(meta);
            wand = item;
        }
    
    }
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Well, I don't understand what the init function does. I just followed a tutorial on how to do it.
     
  10. Offline

    timtower Administrator Administrator Moderator

    @Akilogramofstone It makes "wand" to stop being null.
    So you need to call it once.
     
  11. Thanks for the help, it worked!
     
Thread Status:
Not open for further replies.

Share This Page