1
public void putExpensiveWineCaseBack(double notBiggerThan)
{
   Iterator<WineCase> it = basket.iterator();
   WineCase checkedWineCase = null;
   while( it.hasNext() )
   {
       checkedWineCase = it.next();
       double checkedPrice = checkedWineCase.getPrice();
       int i=0;
       for(i=0; i<basket.size(); i++)
        {
        if(checkedPrice > notBiggerThan)
        {
            basket.remove(i);
        }
      }
    }
} 

}

This code is compiling. The problem is that when executed I get this error:

java.util.ConcurentModificationexception:
null(in java.util.ArrayList$Itr) 

for this line:

checkedWineCase = it.next();

What am I missing ?

1
  • And the code for winecase? Commented Dec 4, 2013 at 19:08

6 Answers 6

1

Change from

  basket.remove(i);

to

   it.remove();
Sign up to request clarification or add additional context in comments.

Comments

0

You're calling basket.remove() but should call it.remove() instead.

Comments

0

You are removing items from the list in the loop where you are iterating through the items in the list. That is the cause of the ConcurrentModificationException.

Comments

0

'null pointer' does not exactly describe your problem, rather it is the name of the exception you received: ConcurentModificationexception

When an iterator is instantiated, it provides access to a certain group of objects, in this case, the contents of 'basket'. It might take some time to go from getting the first object in the iterator to getting the last one.

Now, what happens if the objects in the 'basket' change? The iterator is designed to be 'fail-fast', meaning if the basket has changed, you will immediately get an exception the next time you try to use the Iterator.

This is happening because in some cases you call 'basket.remove()' before you have finished iterating over everything in the basket. You may wish to 'remember' which things should be removed from the basket, and then remove them only when you are completely done with the iterator.

Google 'java iterator concurrentmodificationexception' to see many more explanations of the issue you are encountering.

Comments

0

The problem is you are removing elements from basket while you are iterating over it.

Actually I'm not sure what you are trying to do, as that for loop makes not much sense to me. But I guess you want to remove the element from the Set if the price is larger than notBiggerThan.

So maybe you should try like this:

while(it.hasNext()) {
   checkedWineCase = it.next();
   double checkedPrice = checkedWineCase.getPrice();
   if(checkedPrice > notBiggerThan)
   {
     it.remove();
   }
}

Comments

0

You should not modify the collection while iterating, except by way of Iterator.remove().

The problem comes from your call to basket.remove(i);

I do not completely understand what you are trying to do with that inner for loop (currently it looks like it will remove everything from your basket if any have a price > than the maximum), maybe you want the following:

public void putExpensiveWineCaseBack(double notBiggerThan)
{
   Iterator<WineCase> it = basket.iterator();
   WineCase checkedWineCase = null;
   while( it.hasNext() )
   {
       checkedWineCase = it.next();
       double checkedPrice = checkedWineCase.getPrice();

       if(checkedPrice > notBiggerThan)
       {
           it.remove();
       }

    }
} 

1 Comment

this would not have the same functionality, as he is not removing the element he just got from the iterator.

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.