Solved ArrayIndexOutOfBounds, when length is checked

Discussion in 'Plugin Development' started by Colby l, Feb 13, 2014.

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

    Colby l

    I'm trying to add players to a fly ban list, and the arguments can either have a time, or no time, and be considered permanent.

    I check for the args' length before initializing it to a variable, but when trying to use the variable, the ArrayIndexOutOfBounds is still thrown.

    Code:
    Code:java
    1. } else if (args[0].equalsIgnoreCase("ban")) {
    2. // permission check
    3. if (!(p.hasPermission("FlyP.ban.add"))) {
    4. // Sends player no permission message
    5. p.sendMessage(Messages.permissionErrorMessage(p));
    6. // returns true
    7. return true;
    8. }
    9.  
    10. // Arg amount check
    11. if (args.length >= 2) {
    12. // converts banned player argument to string
    13. String bannedPlayer = Array.get(args, 1).toString();
    14. //Checks if player is online
    15. if (getServer().getPlayerExact(bannedPlayer) == null) {
    16. //sends player message player is not online
    17. p.sendMessage(ChatColor.RED + Messages.playerOfflineMessage(bannedPlayer, p));
    18. //ends method of false
    19. return true;
    20. }
    21. // checks if player is exempt from being banned
    22. if (getServer().getPlayer(bannedPlayer).hasPermission("FlyP.ban.protect")) {
    23. // tells command sender player was not banned because
    24. // protected from fly ban
    25. p.sendMessage(ChatColor.RED + Messages.playerProtectedFromBanMessage(p, bannedPlayer));
    26. // ends method
    27.  
    28. return true;
    29. }
    30. // converts time arg to string
    31. String timeBanned = Array.get(args, 2).toString();
    32. // Converts time banned to milliseconds
    33. long timeBannedMilliseconds = Utilities.parseStringToMilliseconds(timeBanned);
    34. // checks max ban time
    35. if (BanList.hasMaxBanTime()) {
    36. // checks if ban is longer than max time
    37. if (timeBannedMilliseconds > BanList.getMaxBanTime()) {
    38. // sends player message saying ban was too long
    39. p.sendMessage(ChatColor.RED + Messages.maxBanTimeReachedMessage(p));
    40. // returns true, ends method
    41. return true;
    42. // if ban time is within max ban time limit, runs ban
    43. // list add
    44. } else {
    45.  
    46. // adds player to ban list
    47. BanList.addPlayerBanList(bannedPlayer, timeBanned);
    48. //Sends player message saying person was banned
    49. p.sendMessage(ChatColor.GREEN + Messages.banAddedMessage(p, bannedPlayer, timeBanned));
    50. //ends method
    51. return true;
    52. }
    53.  
    54. // if no max ban, then runs ban list add
    55. } else {
    56.  
    57. // adds player to ban list
    58. BanList.addPlayerBanList(bannedPlayer, timeBanned);
    59. //Sends player message saying person was banned
    60. p.sendMessage(ChatColor.GREEN + Messages.banAddedMessage(p, bannedPlayer, timeBanned));
    61. //ends method
    62. return true;
    63.  
    64. }
    65.  
    66. // doesn't include time
    67. } else if (args.length >= 1) {
    68. //checks if permanent bans are allowed
    69. if (!BanList.hasMaxBanTime()){
    70. //sends player message requiring to set a ban time
    71. p.sendMessage(ChatColor.RED + Messages.requiredBanTimeCommandMessage(p));
    72. //ends method
    73. return true;
    74. //if permanent bans are allowed
    75. } else {
    76. //banned player
    77. String bannedPlayer = Array.get(args, 1).toString();
    78. //time banned permanent
    79. String timeBanned = ("9999999999999999M");
    80. // adds player to ban list
    81. BanList.addPlayerBanList(bannedPlayer, timeBanned);
    82. //Sends player message saying person was banned
    83. p.sendMessage(ChatColor.GREEN + Messages.banAddedMessage(p, bannedPlayer, timeBanned));
    84. //ends method
    85. return true;
    86.  
    87. }
    88.  
    89. }
    90.  
    91. }


    When I try '/fly ban ahellhound' I get this error:
    Code:
    Caused by: java.lang.ArrayIndexOutOfBoundsException
        at java.lang.reflect.Array.get(Native Method) ~[?:1.7.0_51]
        at com.ahellhound.bukkit.flypayment.Main.onCommand(Main.java:324) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[CB_DV_3005.jar:git-Bukkit-1.7.2-R0.2-36-g9f75167-b3005jnks]
        ... 16 more
    line '324' is this: 'String timeBanned = Array.get(args, 2).toString();'

    But, that line should be unreachable when using "1" (index 0, and 1) arguments.

    Thanks for the help!
     
  2. Offline

    adam753

    Checking the length of an array give you the number of eements in it, not the index of the last one. So, in the command '/fly ban ahellhound', args.length is equal to 2, not 1. (Hence why getting element 2 is producing an error.)
     
  3. Offline

    Colby l

    That's what I thought the issue was also, but I played around with the values and the same errors still occur.

    I have no clue whats wrong, I've been at this for 4 hours. I've tried calling the 'args' array from the 'Array' class, by using 'args[1]', and I've printed all the args with the corresponding numbers to check if the correct args were being called. Still receive an OutOfBounds error...

    EDIT: Here's the updated code:

    Code:java
    1. // fly ban
    2. } else if (args[0].equalsIgnoreCase("ban")) {
    3. // permission check
    4. if (!(p.hasPermission("FlyP.ban.add"))) {
    5. // Sends player no permission message
    6. p.sendMessage(Messages.permissionErrorMessage(p));
    7. // returns true
    8. return true;
    9. }
    10.  
    11. // Arg amount check
    12. if (args.length == 2) {
    13. // converts banned player argument to string
    14. String bannedPlayer = args[1];
    15. //Checks if player is online
    16. if (getServer().getPlayerExact(bannedPlayer) == null) {
    17. //sends player message player is not online
    18. p.sendMessage(ChatColor.RED + Messages.playerOfflineMessage(bannedPlayer, p));
    19. //ends method of false
    20. return true;
    21. }
    22. // checks if player is exempt from being banned
    23. if (getServer().getPlayer(bannedPlayer).hasPermission("FlyP.ban.protect")) {
    24. // tells command sender player was not banned because
    25. // protected from fly ban
    26. p.sendMessage(ChatColor.RED + Messages.playerProtectedFromBanMessage(p, bannedPlayer));
    27. // ends method
    28.  
    29. return true;
    30. }
    31. // converts time arg to string
    32. String timeBanned = args[2];
    33. // Converts time banned to milliseconds
    34. long timeBannedMilliseconds = Utilities.parseStringToMilliseconds(timeBanned);
    35. // checks max ban time
    36. if (BanList.hasMaxBanTime()) {
    37. // checks if ban is longer than max time
    38. if (timeBannedMilliseconds > BanList.getMaxBanTime()) {
    39. // sends player message saying ban was too long
    40. p.sendMessage(ChatColor.RED + Messages.maxBanTimeReachedMessage(p));
    41. // returns true, ends method
    42. return true;
    43. // if ban time is within max ban time limit, runs ban
    44. // list add
    45. } else {
    46.  
    47. // adds player to ban list
    48. BanList.addPlayerBanList(bannedPlayer, timeBanned);
    49. //Sends player message saying person was banned
    50. p.sendMessage(ChatColor.GREEN + Messages.banAddedMessage(p, bannedPlayer, timeBanned));
    51. //ends method
    52. return true;
    53. }
    54.  
    55. // if no max ban, then runs ban list add
    56. } else {
    57.  
    58. // adds player to ban list
    59. BanList.addPlayerBanList(bannedPlayer, timeBanned);
    60. //Sends player message saying person was banned
    61. p.sendMessage(ChatColor.GREEN + Messages.banAddedMessage(p, bannedPlayer, timeBanned));
    62. //ends method
    63. return true;
    64.  
    65. }
    66.  
    67. // doesn't include time
    68. } else if (args.length == 1) {
    69. //checks if permanent bans are allowed
    70. if (!BanList.hasMaxBanTime()){
    71. //sends player message requiring to set a ban time
    72. p.sendMessage(ChatColor.RED + Messages.requiredBanTimeCommandMessage(p));
    73. //ends method
    74. return true;
    75. //if permanent bans are allowed
    76. } else {
    77. //banned player
    78. String bannedPlayer = args[1];
    79. //time banned permanent
    80. String timeBanned = ("9999999999999999M");
    81. // adds player to ban list
    82. BanList.addPlayerBanList(bannedPlayer, timeBanned);
    83. //Sends player message saying person was banned
    84. p.sendMessage(ChatColor.GREEN + Messages.banAddedMessage(p, bannedPlayer, timeBanned));
    85. //ends method
    86. return true;
    87.  
    88. }
    89.  
    90. }
    91.  
    92. } else {
    93. p.sendMessage(ChatColor.RED + Messages.invalidArgumentMessage(p));
    94. return true;
    95. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  4. Offline

    adam753

    Colby l
    Exactly the same problem - getting args[1] after checking that args.length is exactly 1 is always going to be in error. If the length is 1, there is only one object in the list, which is args[0]. But since you've set the thread to solved, I'm going to assume you got this yourself.
     
Thread Status:
Not open for further replies.

Share This Page