1

I am working in vaadin framework. I have an absolute Layout and I added some components to that layout, now i am iterating absolute layout to get all the components from the absolute layout and at the same time i am removing one or more components from the same absolute layout. but it gives me concurrent modification exception. help me how to avoid the exception. the requirement is to remove the components from the absolute layout while iterating same layout.

Code that generate exception:

Iterator<?> iterate = absoluteLayout.getComponentIterator();
    while (iterate.hasNext()) 
    {
        Component c = (Component) iterate.next();
        if(c instanceof Button)
        {
            Button button = (Button) c;
            if(button.getCaption().equals(""))
            {
                Long id = (Long) button.getData();
                if(id == subSystemId)
                {
                    flag = true;
                    absoluteLayout.removeComponent(button);
                    absoluteLayout.removeComponent(tempLabel);
                    absoluteLayout.removeComponent(tempOptionGroup);

                    System.out.println("Waooo!! Components has been removed fro absolute layout!!");
                }
            }
        }
        else if(c instanceof Label)
        {
            tempLabel = (Label) c;
        }
        else if(c instanceof OptionGroup)
        {
            tempOptionGroup = (OptionGroup) c;
        }
    }

the exception is:

java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:394)
at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:405)
at com.medmax.Dashboard.cchpi.ROSComponent.removeRosSubSystemLogic(ROSComponent.java:401)

.......

codeling
  • 11,056
  • 4
  • 42
  • 71

1 Answers1

1

You are iterating and removing from the absoluteLayout inside the same loop. Instead try declaring the variables like tempLabel, tempOptionGroup, and button outside while loop.

It should solve your problem

Arunkumar
  • 3,812
  • 3
  • 22
  • 30