Trouble when creating ItemStack inside a for loop.

Discussion in 'Plugin Development' started by Paul122, Aug 16, 2020.

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



    I've been stuck on this issue for days now. Essentially what I'm trying to do is to create a new item stack every time the loop iterates through an item in the config, and add it to an array list as seen below:

    private List<ItemStack> items = new ArrayList<>();
    for (String reward : section.getKeys(false)) {
                    String name = section.getString(reward + ".DisplayName");
                    String id = section.getString(reward + ".DisplayItem");
                    ItemStack item = new ItemStack(Material.matchMaterial(id));
                    ItemMeta itemMeta = item.getItemMeta();

    However, when trying to access the list outside of the loop, and attempting to print 'items.get(0).getItemMeta().getDisplayName();' nothing is printed, and an out of bounds exception is thrown (which leads me to believe that nothing is being added to the list). I know that nothing is wrong with the config or the loop, since when I try to print either the "name" or "id" Strings alone inside of the loop, they are printed fine.
  2. Offline


    @Paul122 How many keys does the section have? It might be empty. If you have something in the file, you might be getting the path wrong.
  3. Offline


    There are two items under the "Keys" section, and I'm almost certain the path is correct since I can use the same path to access abd print the Strings on their own, without adding them to an ItemStack variable.
  4. Offline


    @Paul122 Oh, so you can print the keys inside the loop? If that's the case, i'm not sure how it's not getting added when you don't continue, break, throw or return.

    Try debugging.
  5. Offline


    @KarimAKL Yeah, I tried debugging last night and didn't really find anything. This is part of the reason why I'm so perplexed, it should be working.


    I'm now able to print the item data within the for loop itself, by doing:

    This successfully prints the corresponding String from config, but it still doesn't look like adding the item to an ArrayList is working. When I try to print

    It throws and out of bounds exception. Any insight into what could be causing this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Aug 16, 2020
Thread Status:
Not open for further replies.

Share This Page