Newbie at Coding: Frustrating Error

Discussion in 'Plugin Development' started by adampoconnor, Jul 5, 2012.

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

    adampoconnor

    Hello guys. I am a newbie at coding java, and I wanted to make pages within a code that I had. Here is the server log:
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'imperiallegion' in plugin ERPGCore v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at me.adampoconnor.erpgcore.erpgrestart.onCommand(erpgrestart.java:38)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    ... 12 more
    

    Here is the affected code.
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player)sender;
            if(cmd.getName().equalsIgnoreCase("imperiallegion")){
                if(args.length == 0) {
                player.sendMessage(ChatColor.GOLD + "      Imperial Legion Info");
                player.sendMessage(ChatColor.GOLD + "----------------------------------");
                player.sendMessage(ChatColor.RED + "/imperiallegion ranks - Shows the current ranks in Imperial Legion");
                player.sendMessage(ChatColor.RED + "/imperiallegion members - Shows the current members of Imperial Legion");
                }
                else {
                    if(args[0].equalsIgnoreCase("ranks")) {
                        if(args[1].equalsIgnoreCase("4")){
                        player.sendMessage(ChatColor.GOLD + "      Imperial Legion Info");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "            Worker ranks:");
                        player.sendMessage(ChatColor.DARK_RED + "Venator");
                        player.sendMessage(ChatColor.DARK_RED + "Worker II");
                        player.sendMessage(ChatColor.DARK_RED + "Worker");
                    }
                        if(args[1].equalsIgnoreCase("3")){
                        player.sendMessage(ChatColor.GOLD + "      Imperial Legion Info");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "          Soldier ranks:");
                        player.sendMessage(ChatColor.DARK_RED + "Evocati");
                        player.sendMessage(ChatColor.DARK_RED + "Munifex");
                        player.sendMessage(ChatColor.DARK_RED + "Miles");
                        player.sendMessage(ChatColor.GOLD + "Type /imperiallegion ranks 4 to see the next page.");
                    }
                        if(args[1].equalsIgnoreCase("2")){
                        player.sendMessage(ChatColor.GOLD + "      Imperial Legion Info");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "        The Guards Ranks:");
                        player.sendMessage(ChatColor.DARK_RED + "Royal Guard");
                        player.sendMessage(ChatColor.DARK_RED + "Guard");
                        player.sendMessage(ChatColor.DARK_RED + "Watchman");
                        player.sendMessage(ChatColor.GOLD + "Type /imperiallegion ranks 3 to see the next page.");
                }
                        if(args[1].isEmpty()) {
                        player.sendMessage(ChatColor.GOLD + "      Imperial Legion Info");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "            Leader Ranks:");
                        player.sendMessage(ChatColor.DARK_RED + "Emporer");
                        player.sendMessage(ChatColor.DARK_RED + "General");
                        player.sendMessage(ChatColor.DARK_RED + "Leiutenant");
                        player.sendMessage(ChatColor.GOLD + "Type /imperiallegion ranks 2 to see the next page.");
                }
                    return true;
                    }
                    else if(args[0].equalsIgnoreCase("members")){
                        if(args[1].equalsIgnoreCase("2")){
                        player.sendMessage(ChatColor.GOLD + "      Imperial Legion Info");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "Current Members in Imperial Legion:");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.YELLOW + "Soldiers:");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "Miles:");
                        player.sendMessage(ChatColor.DARK_RED + "ZimZam97");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.YELLOW + "Workers:");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "Worker:");
                        player.sendMessage(ChatColor.DARK_RED + "Moof_Masterson");
                        player.sendMessage(ChatColor.DARK_RED + "kjcampbell3333");
                        player.sendMessage(ChatColor.DARK_RED + "blacknj");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        }
                        if(args[1].isEmpty()) {
                        player.sendMessage(ChatColor.GOLD + "      Imperial Legion Info");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "Current Members in Imperial Legion:");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.YELLOW + "Leaders:");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "Emperor:");
                        player.sendMessage(ChatColor.DARK_RED + "Detester");
                        player.sendMessage(ChatColor.DARK_RED + "Darthfreezen");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.YELLOW + "Guards:");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.RED + "Watchman:");
                        player.sendMessage(ChatColor.DARK_RED + "Ravenblade96");
                        player.sendMessage(ChatColor.DARK_RED + "Bunnibooh123");
                        player.sendMessage(ChatColor.GOLD + "----------------------------------");
                        player.sendMessage(ChatColor.GOLD + "Type /imperiallegion members 2 to see the next page."); }
                        return true;
                    }
                }
            }
            return false;
        }
    }
    Any help is appreciated. Keep in mind that I am a newbie so please try not to yell at me too much.

    Regards,

    Adam :)
     
  2. Offline

    EnvisionRed

    ArrayIndexOutOfBounds exception is thrown when you try to use an item in an array that isn't existant, i.e. args[1].
    Rather than testing if args[1].isEmpty which isn't going to return anything of use to you, if a person just puts blank space after the first argument, args[0] is still the only argument. Just check args.length == 1.
     
  3. Offline

    adampoconnor

    Sorry but I am still confused in what you are saying. How would I use if(args.length == 1) { }?
     
  4. Offline

    nehuskerfan2

    Try this.
    Code:java
    1.  
    2. [FONT=Consolas]public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    3. Player player = (Player)sender;
    4. if(cmd.getName().equalsIgnoreCase("imperiallegion")){
    5. if(args.length == 0) {
    6. player.sendMessage(ChatColor.GOLD + " Imperial Legion Info");
    7. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    8. player.sendMessage(ChatColor.RED + "/imperiallegion ranks - Shows the current ranks in Imperial Legion");
    9. player.sendMessage(ChatColor.RED + "/imperiallegion members - Shows the current members of Imperial Legion");
    10. } else if(args.length >= 1 && args.length < 3 {
    11. if(args[0].equalsIgnoreCase("ranks")) {
    12. if(args[1].equalsIgnoreCase("4")){
    13. player.sendMessage(ChatColor.GOLD + " Imperial Legion Info");
    14. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    15. player.sendMessage(ChatColor.RED + " Worker ranks:");
    16. player.sendMessage(ChatColor.DARK_RED + "Venator");
    17. player.sendMessage(ChatColor.DARK_RED + "Worker II");
    18. player.sendMessage(ChatColor.DARK_RED + "Worker");
    19. }
    20. if(args[1].equalsIgnoreCase("3")){
    21. player.sendMessage(ChatColor.GOLD + " Imperial Legion Info");
    22. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    23. player.sendMessage(ChatColor.RED + " Soldier ranks:");
    24. player.sendMessage(ChatColor.DARK_RED + "Evocati");
    25. player.sendMessage(ChatColor.DARK_RED + "Munifex");
    26. player.sendMessage(ChatColor.DARK_RED + "Miles");
    27. player.sendMessage(ChatColor.GOLD + "Type /imperiallegion ranks 4 to see the next page.");
    28. }
    29. if(args[1].equalsIgnoreCase("2")){
    30. player.sendMessage(ChatColor.GOLD + " Imperial Legion Info");
    31. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    32. player.sendMessage(ChatColor.RED + " The Guards Ranks:");
    33. player.sendMessage(ChatColor.DARK_RED + "Royal Guard");
    34. player.sendMessage(ChatColor.DARK_RED + "Guard");
    35. player.sendMessage(ChatColor.DARK_RED + "Watchman");
    36. player.sendMessage(ChatColor.GOLD + "Type /imperiallegion ranks 3 to see the next page.");
    37. }
    38. if(args[1].isEmpty()) {
    39. player.sendMessage(ChatColor.GOLD + " Imperial Legion Info");
    40. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    41. player.sendMessage(ChatColor.RED + " Leader Ranks:");
    42. player.sendMessage(ChatColor.DARK_RED + "Emporer");
    43. player.sendMessage(ChatColor.DARK_RED + "General");
    44. player.sendMessage(ChatColor.DARK_RED + "Leiutenant");
    45. player.sendMessage(ChatColor.GOLD + "Type /imperiallegion ranks 2 to see the next page.");
    46. }
    47. return true;
    48. }
    49. else if(args[0].equalsIgnoreCase("members")){
    50. if(args[1].equalsIgnoreCase("2")){
    51. player.sendMessage(ChatColor.GOLD + " Imperial Legion Info");
    52. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    53. player.sendMessage(ChatColor.RED + "Current Members in Imperial Legion:");
    54. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    55. player.sendMessage(ChatColor.YELLOW + "Soldiers:");
    56. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    57. player.sendMessage(ChatColor.RED + "Miles:");
    58. player.sendMessage(ChatColor.DARK_RED + "ZimZam97");
    59. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    60. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    61. player.sendMessage(ChatColor.YELLOW + "Workers:");
    62. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    63. player.sendMessage(ChatColor.RED + "Worker:");
    64. player.sendMessage(ChatColor.DARK_RED + "Moof_Masterson");
    65. player.sendMessage(ChatColor.DARK_RED + "kjcampbell3333");
    66. player.sendMessage(ChatColor.DARK_RED + "blacknj");
    67. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    68. }
    69. if(args[1].isEmpty()) {
    70. player.sendMessage(ChatColor.GOLD + " Imperial Legion Info");
    71. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    72. player.sendMessage(ChatColor.RED + "Current Members in Imperial Legion:");
    73. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    74. player.sendMessage(ChatColor.YELLOW + "Leaders:");
    75. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    76. player.sendMessage(ChatColor.RED + "Emperor:");
    77. player.sendMessage(ChatColor.DARK_RED + "Detester");
    78. player.sendMessage(ChatColor.DARK_RED + "Darthfreezen");
    79. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    80. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    81. player.sendMessage(ChatColor.YELLOW + "Guards:");
    82. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    83. player.sendMessage(ChatColor.RED + "Watchman:");
    84. player.sendMessage(ChatColor.DARK_RED + "Ravenblade96");
    85. player.sendMessage(ChatColor.DARK_RED + "Bunnibooh123");
    86. player.sendMessage(ChatColor.GOLD + "----------------------------------");
    87. player.sendMessage(ChatColor.GOLD + "Type /imperiallegion members 2 to see the next page."); }
    88. return true;
    89. }
    90. }
    91. }
    92. return false;
    93. }
    94. }[/FONT]
    95.  
     
  5. Offline

    adampoconnor

    Didn't work sorry.

    This error comes up

    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at me.adampoconnor.erpgcore.erpgrestart.onCommand(erpgrestart.java:37)
     
  6. Offline

    nehuskerfan2

    Look at line 37 in Eclipse and tell me what it says.
    I am too a newbie at this programming and I'm also developing a plugin. I have an error though. I'm trying to help others until someone gets me help.
     
  7. Offline

    adampoconnor

    if(args[1].equalsIgnoreCase("4")){

    I suspect that this line in question, is not the actual error. I believe it is something else related..
     
  8. Offline

    EnvisionRed

    >.<
    Look at your stack trace. Then look at this: http://forums.bukkit.org/threads/ho...ubleshoot-your-own-plugins-by-yourself.32457/
    Now look at your stack trace. You'll see that there's an error at the line
    Code:
    if (args[1].isEmpty()) {
    //send a lot of messages to the player, poor guy.
    }
    The issue is that you're trying to check if args[1] is empty, when args[1] doesn't exist! And when you try to call an element in an array that doesn't exist (like args[1]) you get an ArrayIndexOutOfBoundsException!
    So replace it with
    Code:
    if (args.length == 1){
    //send a lot of messages to the player, he never even knew what was coming :(
    }
     
  9. Offline

    adampoconnor

    I finally understand now! Thank you so much! <3


    EDIT: Same error is thrown. I replaced if(args[1].isEmpty){} with if(args.length==1).

    Sorry if I look like a complete idiot but I just don't see what's wrong. ._.
     
  10. Offline

    EnvisionRed

    I'm noticing that you call if(args[1].Empty()) 2 times in your code. Once at line 38 and once at line 69 or so. Make sure you have replaced that as well?
     
  11. Offline

    adampoconnor

    I reverted to my old code, before nehuskerfan posted. Those 2 lines are what I just changed.

    Regards,

    Adam
     
  12. Offline

    sayaad

    You sound like the old spice guy xD

    Anyway... I see that there are 2 instances of

    Code:java
    1. if(args[1].isEmpty()) {

    Did you replace both with :
    Code:java
    1. if (args.length == 1){


    Other than that and you still get the error then learn to read stack traces and then tell me which line the error in on.

    Edit :
    The above messages did not appear until I hit the post button so don't blame me for not reading it before I posted this.
     
  13. Offline

    adampoconnor

    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at me.adampoconnor.erpgcore.erpgrestart.onCommand(erpgrestart.java:38)

    Line in question:
    if(args[1].equalsIgnoreCase("4")){



    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at me.adampoconnor.erpgcore.erpgrestart.onCommand(erpgrestart.java:76)


    Line in question:

    if(args[1].equalsIgnoreCase("2")){
     
  14. Offline

    EnvisionRed

    Again, look at the type of error you're getting, and the what the lines of code include. Also, sayaad, That was the intention :3
     
  15. Offline

    adampoconnor

    So my guess is, that args[1] somehow don't exist? I don't get it. If I type /imperiallegion ranks I get the internal error. If I type /imperiallegion ranks 4 (one line that somehow has an 'error') it works just fine...
     
  16. args¨1* dont exist when there was only 1 argument, t is existing when there are 2 or more arguments
     
  17. Offline

    adampoconnor

    That didn't answer what I am trying to fix...
     
  18. Offline

    chaseoes

    Yes it did.. you're trying to use args[1] when it does not exist.

    /imperiallegion ranks 4
    /command args[0] args[1]

    So if you leave the 4 off all you have is an args[0], the word "ranks".
     
  19. Offline

    adampoconnor

    But you are not understanding.

    THE ERROR SAYS IT'S ON /imperiallegion ranks 4 WHEN IT'S NOT. I THEN TRIED TO USE if(args.length==1) BUT IT DOESN'T WORK. OKAY?


    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at me.adampoconnor.erpgcore.erpgrestart.onCommand(erpgrestart.java:38)

    Line in question:
    if(args[1].equalsIgnoreCase("4")){
     
  20. Offline

    chaseoes

    And what command are you typing?
     
  21. Offline

    adampoconnor

    /imperiallegion ranks


    EDIT: I fixed it myself. Thanks for all the help..
     
  22. Offline

    chaseoes

    So what I said still stands true... you're not supplying an args[1] with that command.
    You're trying to do:
    Code:
    if(args[1].equalsIgnoreCase("4")){
    When you're not giving it an args[1]. So first check if args.length is greater than or equal to 1.
     
  23. Offline

    sayaad

    Why are you raging over your own mistake? If you re-read you will realize that is the exact fix for the problem specified.
     
  24. You're checking if there is 1 element in the args array yet you're getting the 2nd... arrays start from 0, so 1 is the 2nd element. :)
    Check if args.length is > 1 or >= 2.
     
Thread Status:
Not open for further replies.

Share This Page