Solved ArrayList

Discussion in 'Spigot Plugin Development' started by MadMaxCookie, May 27, 2016.

  1. Hello so I get this error when I do the command + args[0] list

    Code (Java):
    [00:32:15 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'cr'
    in plugin CustomRecipe v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServe
    r.java:645) ~[spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1350) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java
    :1185) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
            at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-lat
    est.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:7
    21) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:4
    00) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:6
    60) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java
    :559) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_91]
            at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_91]
            at org.madmaxcookie.CommandCr.onCommand(CommandCr.java:84) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-latest.jar:git-Spigot-e6f93f4-ed60c01]
            ... 15 more
    I have this code

    Code (Java):
    if(args[0].equalsIgnoreCase("list")) {
                        if(!Permitted("customrecipe.list", p)) {
                            p.sendMessage(prefix("Error") + "Not permitted to do that");
                            return true;
                        }
                        Set<String> s = new HashSet<String>();
                        if(ca.getConfig().getKeys(false) == null) {
                            p.sendMessage(prefix("Error") + "There are no recipes to be listed");
                        }else {
                            s.addAll(ca.getConfig().getKeys(false));
                        }
                        List<String> list = new ArrayList<String>(s);
                        int warps = list.size();
                        for(int i = 0; i < items + 1; i++) {
                            for(String b : Arrays.asList(list.get(i))) {
                                p.sendMessage(colorize("&6&l>&e&l> &8: &7" + b));
                            }
                        }
                        p.sendMessage(" ");
                        if(warps >= items) {
                            p.sendMessage(prefix("Success") + "Page 1 / " + warps / items + (list.size() % items));
                        }else {
                            p.sendMessage(prefix("Success") + "Page 1 / 1");
                        }
                        return true;
                    }
     
  2. Is there somewhere where you try to reference the 4th element of an array with an index of 4?
     
  3. like this one ?
    Code (Java):
    for(String b : Arrays.asList(list.get(i))) {
     
  4. Indexes start at 0 and end at Length -1. if you remove the +1 in your for loop this will fix the problem.
    Edit: yes a enhanced for loop would work.
     
  5. I fixed the loop with this working fine and sending the list, but there are still error
    Code (Java):
    for(int i = 0; i < (items + 1); i++) {
    Code (Java):
    [00:42:18 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'cr'
    in plugin CustomRecipe v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServe
    r.java:645) ~[spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1350) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java
    :1185) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
            at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-lat
    est.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:7
    21) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:4
    00) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:6
    60) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java
    :559) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_91]
            at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_91]
            at org.madmaxcookie.CommandCr.onCommand(CommandCr.java:84) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-latest.jar:git-Spigot-e6f93f4-ed60c01]
            ... 15 more
    >
     
  6. the line 84
    Code (Java):
    for(String b : Arrays.asList(list.get(i))) {
     
  7. You must remove the +1 in your for loop.
    Code (Text):
    for(int i = 0; i < items; i++) {
     
  8. Code (Java):
    [00:47:01 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'cr'
    in plugin CustomRecipe v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServe
    r.java:645) ~[spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1350) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java
    :1185) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
            at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-lat
    est.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:7
    21) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:4
    00) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:6
    60) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java
    :559) [spigot-latest.jar:git-Spigot-e6f93f4-ed60c01]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_91]
            at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_91]
            at org.madmaxcookie.CommandCr.onCommand(CommandCr.java:84) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-latest.jar:git-Spigot-e6f93f4-ed60c01]
            ... 15 more
    >
     
  9. The problem is you are setting your items variable to a number higher then the number of objects in that arraylist. Use ArrayList#.size();
    Code (Text):
    for(int i = 0; i < ArrayList#size(); i++) {
     
  10. I am setting the limit 6 items per list.

    Edit : Fixed by trying some codes.
     
    #13 MadMaxCookie, May 27, 2016
    Last edited: May 27, 2016
  11. 4chan

    Supporter

    You hear that, guys? He finally removed the +1!
     
    • Funny Funny x 1
  12. what ? can you check my new code if I removed + 1 :D

    Code (Java):
    if(args[0].equalsIgnoreCase("list")) {
                        if(!Permitted("customrecipe.list", p)) {
                            p.sendMessage(prefix("Error") + "Not permitted to do that");
                            return true;
                        }
                        Set<String> s = new HashSet<String>();
                        if(ca.getConfig().getKeys(false) == null) {
                            p.sendMessage(prefix("Error") + "No recipes defined");
                        }else {
                            s.addAll(ca.getConfig().getKeys(false));
                        }
                        List<String> list = new ArrayList<String>(s);
                        Collections.sort(list);
                        int warps = list.size();
                        p.sendMessage(colorize("&bCustomRecipe &6&l>&e&l> &8: &7Recipes"));
                        p.sendMessage(colorize("&6&l=====&e&l===== &e&l=====&6&l====="));
                        if(warps >= items) {
                        for(int i = 0; i < (items + 1); i++) {
                            for(String b : Arrays.asList(list.get(i))) {
                                try {
                                p.sendMessage(colorize("&6&l>&e&l> &8: &7" + b));
                                }catch(IndexOutOfBoundsException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        }else {
                            for(int i = 0; i < warps; i++) {
                                for(String b : Arrays.asList(list.get(i))) {
                                    p.sendMessage(colorize("&6&l>&e&l> &8: &7" + b));
                                }
                            }
                        }
                        p.sendMessage(colorize("&6&l=====&e&l===== &e&l=====&6&l====="));
                        if(warps >= items) {
                            p.sendMessage(prefix("Success") + "Page &61 / &6" + warps / items + (list.size() % items));
                        }else {
                            p.sendMessage(colorize("&6&l>&e&l> &8: &7Page &61 &7/ &61 &e&l<&6&l<"));
                        }
                        return true;
                    }
     
  13. 4chan

    Supporter

    Not sure what you did. I still see the +1, however I stopped reading once I noticed "b", "s" and "ca". Bad variable names, loss of interest.

    LO SIENTO.
     
  14. I hope you won't judge more people but I just did is fixed this error called IndexOutOfBoundsException nothing to do with + 1
     
  15. 4chan

    Supporter

    What did you exactly alter though?
    The +1 is usually what causes IOOBE's.
     

  16. get(i) the i is greater than the size of the array list which throws IOOBE

    I did at the loop was

    Code (Java):
    for(int i =0; i < (items +1); i++){
     
  17. 4chan

    Supporter

    Yes, so how did you avoid it?

    Removing the +1 would result in the .get() not being called for the size+1.