0

So this question is a little bit different from the others i've found on here about concurrent exception when modifying the list- because this happens when im modifying an internal list of an object within the list. This is the only method accessing the internal list

Here's where i call the method

public void interactWithItem(int targetIDX, int targetIDY){
    for(Iterator<Item> it = listOfAllItems.iterator(); it.hasNext();){
        Item tempItem = it.next();
        //Maybe i should refine more, in world, so forth
        if(tempItem.tilePosX == targetIDX && tempItem.tilePosY == targetIDY){
            if(tempItem.name.equals("chest")){
                System.out.println("Interacting with Chest!");
                if(!tempItem.containedItems.isEmpty()){
                    for(Iterator<Item> tempIt = tempItem.containedItems.iterator(); tempIt.hasNext();){
                        Item tItem = tempIt.next();
                        System.out.println("Chest contains "+tItem.name+" "+tItem.amount);
                        Character.c.addItem("player", tItem.name, tItem.amount);
                        removeContainedItem("chest", tItem.name);
                    }
                }else{
                    System.out.println("Chest is empty");
                }
            }
        }
    }
}

Here's the method that causes the issue, if i comment out the i.remove(); the issue seizes to happen- so its only upon removal, yet no other method or class is accessing the internal list ?

public void removeContainedItem(String containerName, String itemName){

    System.out.println("Removing "+itemName+" in "+containerName);
    for(Iterator<Item> it = listOfAllItems.iterator(); it.hasNext();){
            Item tItem = it.next();
        if(tItem.name.equals(containerName)){
            for(Iterator<Item> i = tItem.containedItems.iterator(); i.hasNext();){
                Item tempItem = i.next();
                System.out.println(tempItem.name);
                if(tempItem.name.equals(itemName)){
                    i.remove();
                }
            }
        }
    }
}

Thanks for all the help! Hope someone can clarify and give me instructions as to how i might go about fixing this thing? Im a bit at a loss.

Frost
  • 35
  • 6
  • 2
    To get better help post [mcve] which we would actually be able to run and debug the issue on our IDEs. – Pshemo May 11 '18 at 10:20
  • declare your list with CopyOnWriteArrayList (ex: CopyOnWriteArrayList listOfItems = new CopyOnWriteArrayList..it should work – JavaLearner1 May 11 '18 at 10:24
  • What is `listOfAllItems` ? – Yassin Hajaj May 11 '18 at 10:24
  • An arraylist containing item objects, each of these objects have an internal arraylist of its contained items. @JavaLearner1 i will try that, ill let you know if it works. – Frost May 11 '18 at 10:27
  • @Frost sure, please let me know – JavaLearner1 May 11 '18 at 10:31
  • Nope, it throws UnsupportedOperationException at Item.removeContainedItem(Item.java:200) which is (i.remove();) – Frost May 11 '18 at 10:35
  • https://stackoverflow.com/questions/2965747/why-do-i-get-an-unsupportedoperationexception-when-trying-to-remove-an-element-f – JavaLearner1 May 11 '18 at 10:37
  • Concurrent Modification occurs when there is a change in collection when iteration happens..may be you can create another list and add the values which are not equal like - if(!tempItem.name.equals(itemName)) then add to that list..just a try – JavaLearner1 May 11 '18 at 10:38
  • Actually i fixed the issue by turning it into a linkedlist, to be fair im not exactly sure why that worked, ill look into it - but you fixed my issue at any rate! Thankyou! - Could you post as "answer" @JavaLearner1 so i can give you some of these delicious points? – Frost May 11 '18 at 10:49

2 Answers2

1

Concurrent Modification Exception occurs when a collection is modified between the iterations. We can use ConcurrentHashMap or CopyOnWriteArrayList to overcome this issue.

If hitting UnsupportedOperationException, change the ArrayList to LinkedList

JavaLearner1
  • 607
  • 2
  • 8
  • 21
0

Might be :

**Item tItem = tempIt.next();**

When you create an item you then add it to more than one list. And when one of them tries to modify it you can get exceptions. Because bouth lists are using the same item, exactly the same one.

Fix that might help would be :

Item newItem = new Item();
newItem  = tempIt.next();

Simply create new item for each list and modify them as you please.

Or create a new list for :

public void removeContainedItem(String containerName, String itemName){

And modify new copy list with items, then set previous list to modified one.

MartinsB
  • 1
  • 2
  • already fixed it, thanks - that wasnt the issue, but those are some good ideas for people to try with the same issue. So thanks for the input – Frost May 11 '18 at 11:01