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

    Paul122

    Hello,

    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:

    Code:
    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();
                    itemMeta.setDisplayName(name);
                    item.setItemMeta(itemMeta);
                    items.add(item);
      
               
                }

    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

    KarimAKL

    @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

    Paul122

    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

    KarimAKL

    @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

    Paul122

    @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.

    UPDATE:

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

    Code:
    System.out.println(item.getItemMeta().getDisplayName());
    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

    Code:
    System.out.println(items.get(0).getItemMeta().getDisplayName()); 
    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