Solved Commands unhandled exception

Discussion in 'Plugin Development' started by WHQ, Jun 17, 2016.

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

    WHQ

    Hey guys,

    i recently picked up coding again and im kinda new to everything still.
    None of my commands really seem to work, any idea why? Thanks!
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
                Player p = (Player) sender;
                Location ploc = p.getLocation();
                double height2 = p.getLocation().getY();
                double height1 = beginheight.get(p);
                double totalscore = height2 - height1;
                DecimalFormat scoreformat = new DecimalFormat("#,##0.0");
               
               
                ItemStack greenwool = new ItemStack(Material.WOOL, 64, (byte)5 );
                ItemStack brownwool = new ItemStack(Material.WOOL, 64, (byte)12 );
                ItemStack yellowwool = new ItemStack(Material.WOOL, 64, (byte)4 );
                ItemStack redwool = new ItemStack(Material.WOOL, 64, (byte)14 );
                ItemStack helmet = new ItemStack(Material.GOLD_HELMET);
                ItemMeta helmetmeta = helmet.getItemMeta();
               
                Player invite = p.getServer().getPlayer(args[1]);
                   
                if(!(sender instanceof Player))
                    return false;
               
                if(cmd.getName().equalsIgnoreCase("mdj")){
                    if(args.length >= 3){
                        p.sendMessage(ChatColor.DARK_RED+"ERROR: Too many arguments!");
                    }
                    
                    if(args.length == 1){
                         if(args[0].equalsIgnoreCase("start")){
                             start.add(p.getName());
                             p.sendMessage(ChatColor.GREEN + "[" + ChatColor.WHITE + "McDoodleJump" + ChatColor.GREEN + "]" + ChatColor.WHITE + "The game has been started, have fun!" );
                         }
                        
                         if(args[0].equalsIgnoreCase("stop")){
                                start.remove(p.getName());
                                jump.remove(p.getName());
                                p.sendMessage(ChatColor.GREEN + "[" + ChatColor.WHITE + "McDoodleJump" + ChatColor.GREEN + "]" + ChatColor.WHITE + "The game has ended, Your score was:" + ChatColor.ITALIC + scoreformat.format(totalscore));
                         }
                        
                         if(args[0].equalsIgnoreCase("invite") && start.contains(p.getName())){
                             p.sendMessage(ChatColor.DARK_RED + "A man needs a name! try /mdj invite <player>");
                            
                         } else if(args.length == 2)
                            
                         {
                             if(p.getServer().getPlayer(args[1]) != null){
                             invite.teleport(ploc);
                             invite.sendMessage(p.getDisplayName() + " has invited you to the game!");
                             //TODO: Add something to let the invited player choose if he wants to join the game
                             }
    
                         if(args[0].equalsIgnoreCase("build")){
                             p.sendMessage(ChatColor.DARK_RED + "Error: try /mdj build <on/off>");
                             }
                        
                         } else if(args.length == 2)
                        
                         {
                        
                        
                         if(args[1].equalsIgnoreCase("on")){
                             p.sendMessage("Building Mode = On");
                             p.getInventory().clear();
                             p.updateInventory();
                            
                             p.getInventory().addItem(greenwool);
                             p.getInventory().addItem(brownwool);
                             p.getInventory().addItem(yellowwool);
                             p.getInventory().addItem(redwool);
                           
                             p.getInventory().setHelmet(helmet);
                             helmetmeta.setDisplayName("McDoodleJump Building Helmet");
                             helmet.setItemMeta(helmetmeta);
                         }
                        
                         if(args[1].equalsIgnoreCase("off") && (build.contains(p.getName()))){
                             p.sendMessage("Building Mode = Off");
                             p.getInventory().clear();
                             p.updateInventory();
                         }
                         }
                    }
                }
                return true;
            }
    }
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @WHQ plugin.yml?
    And is this is the main class?
    Never get arguments without checking if they exist.
     
  3. Offline

    WHQ

    @timtower
    Code:
    name: McDoodleJump
    main: me.WHQ.McDoodleJump.McDoodleJump
    version: 1.0
    description: >
                 McDoodleJump v1.0
    commands:
      MDJ:
        description: 1234
    yes this is the main class
     
  4. Online

    timtower Administrator Administrator Moderator

    @WHQ Could you post your server log?
     
  5. Offline

    WHQ

    Code:
    [13:33:58] [Server thread/INFO]: Wannes001 issued server command: /mdj start
    [13:33:58] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'mdj' in plugin McDoodleJump v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-4cb3258]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-4cb3258]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:624) ~[craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1298) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1158) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-4cb3258]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:679) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:361) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:635) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:539) [craftbukkit.jar:git-Bukkit-4cb3258]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
        at me.WHQ.McDoodleJump.McDoodleJump.onCommand(McDoodleJump.java:115) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-4cb3258]
        ... 15 more
    [13:34:08] [Server thread/INFO]: Wannes001 issued server command: /mdj stop
    [13:34:08] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'mdj' in plugin McDoodleJump v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-4cb3258]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-4cb3258]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:624) ~[craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1298) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1158) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-4cb3258]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:679) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:361) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:635) [craftbukkit.jar:git-Bukkit-4cb3258]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:539) [craftbukkit.jar:git-Bukkit-4cb3258]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
        at me.WHQ.McDoodleJump.McDoodleJump.onCommand(McDoodleJump.java:115) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-4cb3258]
        ... 15 more
    [13:53:27] [Server thread/INFO]: Wannes001 lost connection: Disconnected
    [13:53:28] [Server thread/INFO]: Wannes001 left the game.
     
  6. Offline

    Lordloss

    well, thats an NPE. You can solve this easily by following the link in my signature. We cant say much without knowing what line 115 is.
     
    WHQ likes this.
  7. Online

    timtower Administrator Administrator Moderator

    @WHQ
    1. You are getting a player before you check if it is a player.
    2. beginheight.get(p); can return null
     
    WHQ likes this.
  8. Offline

    Zombie_Striker

    @WHQ
    I'm assuming since you liked the solutions, that your problem has been solved. Mark this thread as solved if this is the case.
     
Thread Status:
Not open for further replies.

Share This Page