Unhandled exception executing command

Discussion in 'Plugin Development' started by mgbeenieboy, Sep 7, 2014.

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

    mgbeenieboy

    The code:

    Code:java
    1. package me.craftwood.craftwood;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class ExpCommand implements CommandExecutor {
    10.  
    11. @SuppressWarnings("deprecation")
    12. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    13.  
    14. // Umwandlung Sender -> Spieler
    15.  
    16. Player p = null;
    17. if(sender instanceof Player) {
    18. p = (Player) sender;
    19. }
    20.  
    21. // Das zweite Argument ist immer ein Spielername
    22. Player target = Bukkit.getPlayer(args[1]);
    23.  
    24. // Wie viel Exp hab ich?
    25. if(args.length == 0 || (args.length == 1 && args[0] == "show" && args[1] == p.getName())) {
    26. p.sendMessage("§6Du hast §c" + p.getExp() + "§6 Exps, (§c" + p.getLevel()
    27. + "§6 Level). Dir fehlen noch §c" + p.getExpToLevel() + "§6 Exp zum nächsten Level.");
    28. }
    29.  
    30. // Wie viel Exp hat er?
    31. else if(args.length == 2 && args[0] == "show") {
    32. p.sendMessage("§c" + target + "§6 hat §c" + target.getExp() + "§6 Exps, (§c" + target.getLevel()
    33. + "§6 Level). Ihm fehlen noch §c" + target.getExpToLevel() + "§6 Exp zum nächsten Level.");
    34. }
    35.  
    36. // Exp setzen
    37. else if(args.length == 3 && args[0] == "set") {
    38. target.setExp(Float.parseFloat(args[2]));
    39. p.sendMessage("§6Du hast §c" + target + "s §6Exp auf §c" + Float.parseFloat(args[2]) + "§6 gesetzt.");
    40. target.sendMessage("§6Deine Exp wurden auf $c" + Float.parseFloat(args[2]) + "$6 gesetzt.");
    41. }
    42.  
    43. // Exp vergeben
    44. else if(args.length == 3 && args[0] == "give") {
    45. target.giveExp(Integer.parseInt(args[2]));
    46. p.sendMessage("§c" + target + "§6 hat §c" + Float.parseFloat(target.getExp() + args[2])
    47. + "§6 dazu bekommen. Er hat jetzt §c" + target.getExp() + "§6Exp (" + target.getLevel() + "§6 Level)");
    48. }
    49.  
    50. // Exp wegnehmen
    51. else if(args.length == 3 && args[0] == "take") {
    52. target.setExp(target.getExp() - Float.parseFloat(args[2]));
    53. p.sendMessage("§c" + target + "§6 wurden §c" + args[2] + "§6 Exp abgezogen. Er hat jetzt nur noch §c"
    54. + target.getExp() + "§6Exp (" + target.getLevel() + "§6 Level)");
    55. }
    56.  
    57. else {
    58. return false;
    59. }
    60.  
    61. return true;
    62. }
    63.  
    64. }
    65.  


    When I execute the command:
    Code:
    [21:08:44] [Server thread/INFO]: MGbeenieboy issued server command: /exp
    [21:08:44] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'exp' in plugin Craftwood v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1644]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot.jar:git-Spigot-1644]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1644]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1644]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.craftwood.craftwood.ExpCommand.onCommand(ExpCommand.java:21) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-1644]
        ... 13 more
    In game:
    An internal error occured while attempting to perform this command.
     
  2. Offline

    N00BHUN73R

    @mgbeenieboy
    Hello, could you please tell me what's on line 21 of ExpCommand?
    Also my guess would be that you are doing args[1] for the player instead of args[0] because if your command is /exp <player> <exp> it would be <player> = args[0] and exp = args[1]..
    Just my guess.
     
  3. Offline

    mgbeenieboy


    Code:java
    1. p.sendMessage("§6Du hast §c" + p.getExp() + "§6 Exps, (§c" + p.getLevel()

    (the second part of the string is on the next line)

    the syntax is /exp [show|set|give|take] [player] [amount of exp]
    so args[1] would be the player
     
  4. Offline

    N00BHUN73R

    mgbeenieboy
    Did you by chance check if any of these are null?
     
  5. Offline

    mgbeenieboy


    As you can see, at the beginning, I set the variable p to null, because I don't know how to initialize it else... But I'm executing it as a player (in game), so it shouldn't be null anymore...
    Player p =null;
     
  6. Offline

    N00BHUN73R

    mgbeenieboy
    try doing
    Code:java
    1. Player p = (Player)sender;
     
  7. Offline

    mgbeenieboy


    Hm.. it still does not work :(
     
  8. Offline

    mgbeenieboy

    I tested


    Code:java
    1. if(args.length == 1 && args[0] == "debug") {
    2. Bukkit.broadcastMessage("DEBUG");
    3. }



    and I get an internal error on executing the command /exp debug :(


    Seriously... not that I use spigot only because the situation is like it is... but come on, there aren't any official builds anymore. Neither for craftbukkit, nor for spigot and it's kind of illegal to download them. Can't you make an exception here?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  9. mgbeenieboy Until a member of staff with the correct authority says the rules no longer apply, the rules apply.
     
  10. Offline

    mgbeenieboy


    Code:
    [10:45:11] [Server thread/INFO]: MGbeenieboy issued server command: /exp debug
    [10:45:11] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'exp' in plugin Craftwood v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:740) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:957) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:818) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:258) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.craftwood.craftwood.ExpCommand.onCommand(ExpCommand.java:18) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
        ... 13 more
     
  11. Offline

    guitargun

    what is the array and the line at 18?

    edit:
    did you check if
    Code:java
    1. Player target = Bukkit.getPlayer(args[1]);

    that the args is the length 2?
     
  12. Offline

    mgbeenieboy


    which array? line 18 is
    Code:java
    1. if(args.length == 2 && args[0] == "show") {


    And no, I didn't check it. it acutally meant that IF they are are more than two arguments argument 2 would always be a player and the variable target. But I changed to:

    Code:java
    1. package me.craftwood.craftwood;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class ExpCommand implements CommandExecutor {
    10.  
    11. @SuppressWarnings("deprecation")
    12. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    13.  
    14. // Umwandlung Sender -> Spieler
    15. Player p = (Player) sender;
    16.  
    17. if(args.length == 1 && args[0] == "debug") {
    18. Bukkit.broadcastMessage("DEBUG");
    19. }
    20.  
    21. // Wie viel Exp hab ich, hat er?
    22. if(args.length == 2 && args[0] == "show") {
    23. if(args[1] == sender.getName()) {
    24. Player target = Bukkit.getPlayer(args[1]);
    25. p.sendMessage("§6Du hast §c" + target.getExp() + "§6 Exps, (§c" + target.getLevel()
    26. + "§6 Level). Dir fehlen noch §c" + target.getExpToLevel() + "§6 Exp zum nächsten Level.");
    27. } else {
    28. Player target = Bukkit.getPlayer(args[1]);
    29. p.sendMessage("§c" + target + "§6 hat §c" + target.getExp() + "§6 Exps, (§c" + target.getLevel()
    30. + "§6 Level). Ihm fehlen noch §c" + target.getExpToLevel() + "§6 Exp zum nächsten Level.");
    31. }
    32. }
    33.  
    34. // Exp setzen
    35. else if(args.length == 3 && args[0] == "set") {
    36. Player target = Bukkit.getPlayer(args[1]);
    37. target.setExp(Float.parseFloat(args[2]));
    38. p.sendMessage("§6Du hast §c" + target + "s §6Exp auf §c" + Float.parseFloat(args[2]) + "§6 gesetzt.");
    39. target.sendMessage("§6Deine Exp wurden auf $c" + Float.parseFloat(args[2]) + "$6 gesetzt.");
    40. }
    41.  
    42. // Exp vergeben
    43. else if(args.length == 3 && args[0] == "give") {
    44. Player target = Bukkit.getPlayer(args[1]);
    45. target.giveExp(Integer.parseInt(args[2]));
    46. p.sendMessage("§c" + target + "§6 hat §c" + Float.parseFloat(target.getExp() + args[2])
    47. + "§6 dazu bekommen. Er hat jetzt §c" + target.getExp() + "§6Exp (" + target.getLevel() + "§6 Level)");
    48. }
    49.  
    50. // Exp wegnehmen
    51. else if(args.length == 3 && args[0] == "take") {
    52. Player target = Bukkit.getPlayer(args[1]);
    53. target.setExp(target.getExp() - Float.parseFloat(args[2]));
    54. p.sendMessage("§c" + target + "§6 wurden §c" + args[2] + "§6 Exp abgezogen. Er hat jetzt nur noch §c"
    55. + target.getExp() + "§6Exp (" + target.getLevel() + "§6 Level)");
    56. }
    57.  
    58. else {
    59. return false;
    60. }
    61.  
    62. return true;
    63. }
    64.  
    65. }
    66.  


    and I don't get any errors anymore but it always returns false.
     
  13. Offline

    Zarkopafilis



    If the array is null there would be no arguments...not even a number. Check if its null first
     
  14. Offline

    Jadedcat

    Offline servers are not supported.
     
Thread Status:
Not open for further replies.

Share This Page