• Post Reply Bookmark Topic Watch Topic
  • New Topic

[Solved]Inheriting Global variables  RSS feed

 
E Hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a superclass A which has a TreeMap needed to be accessed by A subclass when an event it fired (an unsubscribe notification), there are 4 objects of class B instantiated. However I get ConcurrentModificationException when all the classes access it, Im guessing this is because they are all trying to access the same A.x TreeMap.

Each subclass of A should have its own local copy of the TreeMap, is there anyway to declare this in A or do all subclasses have to declare it so there is no ConcurrentModificationException.

I hope that makes sense, thanks for any help
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually it does not make sense to me,

I dont think you should face a ConcurrentModificationException if there are seperate Objects...

- are you using the same Object ?
- are you using seperate Objects but a static variable containing the collection ?


I would normally recommend to use a synchronized block, but I want to understand your code first....

Please provide a code snippet to help us help you better...
 
E Hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The TreeMap is not a static variable.

Class A has



and Class B:



Theres more code than that but that is all the is resulting in throwing the exception, its not threaded so they aren't accessing the same variable in different threads. The only way I can see it happening is through inheritance.

Thanks
 
steve claflin
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that the code that calls somefunction is passing in x.

Is the body of the loop doing anything to modify the TreeMap (or the pubKeys set)? The forEach loop uses the set's iterator, which will throw an exception on the NEXT use of the iterator after a change to the underlying collection, if the change is not made through the iterator - from TreeMap javadoc:

if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.


Since you are using a forEach loop, you don't actually have access to the iterator it is using, so any changes you make to the collection will by definition not be through the iterator's remove method. Since it is the iterator that throws the exception, it will not be thrown until the next use of it, which is why the exception occurs on the forEach line, rather than within the body of the loop.
 
E Hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks thats sorted it, rather than the forEach loop, I sent it to an Array and used that.

Thanks for all your help.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!