0

I get a weird problem. Getting java.util.ConcurrentModificationException. But I'm not modifying the current collection. Please help.

Method Call

setAllAddress((List<AddressBean>) usrProfileResp.getAddressBeanList());
setShippingAddresses(getAllAddress());

Methods

public List<AddressBean> getAllAddress() {
    return allAddress;
}

public void setShippingAddresses(List<AddressTokenBean> shippingAddresses) {
        shippingAddresses = new ArrayList<AddressBean>();
        List<AddressBean> addresses = getAllAddress();
        if (addresses != null && addresses.size() > 0) { 
            for (AddressBean addr : addresses) { // EXCEPTION OCCURS HERE
                if (!Constants.BILLING_ADDRESS.equals(addr.getAddressType())) {                    
                    shippingAddresses.add(addr);                
                }
            }
        }
}
12
  • Is shippingAddresses the same instance as addresses? Post getAllAddresses() and any more relevant code. Commented Jun 28, 2012 at 7:05
  • I have added the shipping address init in code. Please check. Commented Jun 28, 2012 at 7:06
  • 1
    And getAllAddress()? It's starting to look like shippingAddresses is a class variable and getAllAddress() returns it. Commented Jun 28, 2012 at 7:07
  • 1
    Please show your getAllAddress() implementation. Commented Jun 28, 2012 at 7:10
  • There must be another thread modifying the collection returned by getAllAddress(). Commented Jun 28, 2012 at 7:10

2 Answers 2

1

The most likely explaination is that addresses and shippingAddresses refer to the same collection. You can check this in your debugger.

Does getAllAddresses() use shippingAddresses at all?

Are you sure this collection is not being modifed in another thread? Does this happen all the time or only occasionally?

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

9 Comments

Thanks for your response. But I do initialize shippingAddresses. :(
Not sure what that means, but I assume you are absolutely sure they are not the same object and you have checked in your debugger?
@Peter..Its obvious..he is creating a new Collection
Without the source for getAllAdresses() it not obvious.
@Ahmad obviously there is no bug then. ;)
|
0

I don't really understood why you all guys consider shippingAddresses at all? Vanathi said that the exception occurs while attempting to iterate over the

List addresses = getAllAddress();

in the for-each loop.

So there should be a thread that changes the array referenced by 'addresses'. BTW does the exception happen during the fist iteration or

You implementation of 'getAllAddresses' is as follows:

public List getAllAddress() { return allAddress; }

So I conclude that 'allAddresses' is just a data member of some class. This class obviously allows concurrent modification( another thread? ) This is where you should put your efforts... Just IMO :)

Hope this helps

Comments

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.