-1

How do I fix this code? i don't know what this error means... I heard that it comes from having elements of a list removed during a for each loop, but I don't see anything tha I'm removing...

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        for(Layer e : layerList)
            e.drawLayer(g2);
    }

The jcomponent has a list of objects called layers that it passes Graphics to so the layers can paint themselves. I never remove any of the layers or anything, so I'm lost. Help?

  • 3
    It typically helps to post the stacktrace. As you being to gain more experience, you'll learn to read these and appreciate them for their immense value. – Tim Bender Feb 19 '10 at 05:08
  • You really need to post the stacktrace. Right now, we do not even have the name and message of the exception to work with. – Thilo Feb 19 '10 at 05:51

1 Answers1

1

If you have more then one thread working that could be working with the layerlist, you should consider using a synchronize block as an example below. That will help prevent this problem or you could consider having the layerList be synchronized but with out more info of the program and the thread structure it is hard to tell you what is best. Check out this synchronized list as an option instead of the synchronized block.

 synchronized( layerList ) 
 {
    for(Layer e : layerList)
        e.drawLayer(g2);
 }
Jeff Beck
  • 3,944
  • 3
  • 28
  • 45
  • This is a possible answer; you are accessing layerList which is not thread safe, so using a java.util.Vector or using synchronized will resolve the issue. – CharlesS Aug 03 '12 at 10:31
  • @CharlesS consider moving off Vector if you are still using it see this question as to why [Vector is considered obsolete](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated). – Jeff Beck Aug 03 '12 at 15:09