1.8.8 Need help with ArrayList

Discussion in 'Spigot Plugin Development' started by Azuleee_, Jan 12, 2020.

  1. So i'm trying to make a coins plugin, and i'm trying to make the money top in a gui and i'm getting this with arraylist, and i need some help ;-;

    This is the error:
    Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_191]
    at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_191]
    at net.azulecash.API.CashTop(API.java:62) ~[?:?]

    My code:
    public static List<String> cash = new ArrayList();
    @SuppressWarnings("unchecked")
    public static void CashTop(Player p)
    {
    cash.clear();
    for (String name : Main.getInstance().data.getConfig().getConfigurationSection("Data").getKeys(false)) {
    int quantiaCash = Main.getInstance().data.getInt("Data." + name + ".Coins");;
    String prefix = PermissionsEx.getUser(name).getPrefix().replace("&", "§");
    cash.add(name + ": " + quantiaCash + " §6✪§f de Cash.");

    Collections.sort(cash, new Comparator()
    {
    public int compare(Object arg0, Object arg1)
    {
    int one = Integer.parseInt(((String) arg0).split(" ")[1]);
    int two = Integer.parseInt(((String) arg1).split(" ")[1]) + 1;
    return Integer.compare(one, two);
    }
    });
    Collections.reverse(cash);
    Inventory inv = Bukkit.createInventory(null, 54, "Cash - TOP");

    inv.setItem(11, Criar.add2("§6#§f1§8 -§e " + ((String)cash.get(0)).replace(":", " §fpossui§6" + ""), "donotfight", new String[] { "§6■§e Este jogador(a) está em primeiro lugar!"}, 1));
    inv.setItem(12, Criar.add2("" + ((String)cash.get(1)).replace(":", " §fpossui§6" + ""), "donotfight", new String[] { "§6■§e Este jogador(a) está em terceiro lugar!"}, 1));
    p.openInventory(inv);
    }
    }
    My english is awful so sorry if there's any orthographic error!
     
  2. Arrays start at 0 and then continue on from there.
    This error states that your size is 1, which means there only is one element, 0.
    It says that you are trying to read element 1, which does not exist. It would only exist if you had size 2 or greater. 0,1,...

    int one = Integer.parseInt(((String) arg0).split(" ")[1]); etc is looking for [1], you should probably be looking for [0].
    Just make sure you understand how array size works. If you have size 4, the array will contain [0],[1],[2],[3], but not [4].
     
    • Agree Agree x 1
  3. Thank you so much! i fixed it!
     
    • Like Like x 1