Solved Help with command not running.

Discussion in 'Plugin Development' started by mrdude123, Dec 22, 2015.

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

    mrdude123

    I'm trying to run a command, but in chat I get: An internal error has occurred while trying to perform this command.

    As well as a grotesque stacktrace in the console. Any ideas?
    Code:
    package plugin.thecerealkill3r.bows;
    
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class ArtefactConversion implements CommandExecutor {
    
        public boolean onCommand(CommandSender sender, Command cmd, String lbl,
                String[] args) {
            String lore2 = "test";
            Player player = (Player) sender;
            ItemStack objectinhand = player.getItemInHand();
            List<String> lore = player.getItemInHand().getItemMeta().getLore();
            if(lore.equals(null)){
                sender.sendMessage("This item has no lore.");
                return true;
            }
          
            if(!(lore.equals(lore2))){
                sender.sendMessage(ChatColor.RED + "Conversion failed.");
                return true;
              
            }
            return false;
        }
    
    }
    
    Stacktrace right here:
    Code:
    [18:47:06 INFO]: TheCerealKill3r issued server command: /convertartefact
    [18:47:06 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'conv
    ertartefact' in plugin bowtest v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    1) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:767) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerCon
    nection.java:1016) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :846) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat
    .java:65) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:184
    ) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java
    :81) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    31) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
    Caused by: java.lang.NullPointerException
            at plugin.thecerealkill3r.bows.ArtefactConversion.onCommand(ArtefactConv
    ersion.java:22) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-1.7.10-R0.1-SNAPSHOTBuild1554.jar:git-Spigot-1554]
            ... 13 more
    >
     
  2. Offline

    Xerox262

    .equals() is not null friendly, you cannot call any methods on a null object, to check if an object is null you must use object == null.

    Also, a List<String> can never equal a string, it can however contain it, you need to change your ifs. And come on... Don't cast to player without checking...
     
    Last edited: Dec 22, 2015
    XsergeiX likes this.
  3. Offline

    JoaoBM

    Check if the sender is a player before casting it
    -EDIT-
    Just saw that @Xerox262 mentioned it
     
  4. Offline

    mrdude123

    Derp :p

    Thanks everyone. :)
     
  5. Offline

    Xerox262

    Don't forget to set the thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page