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.

9
  • 2
    To get better help post minimal reproducible example which we would actually be able to run and debug the issue on our IDEs. Commented May 11, 2018 at 10:20
  • declare your list with CopyOnWriteArrayList (ex: CopyOnWriteArrayList<String> listOfItems = new CopyOnWriteArrayList<String>..it should work Commented May 11, 2018 at 10:24
  • What is listOfAllItems ? Commented May 11, 2018 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. Commented May 11, 2018 at 10:27
  • @Frost sure, please let me know Commented May 11, 2018 at 10:31

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.