Results 1 to 6 of 6
  1. #1
    Praijer is offline Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Concurrent Mod Exception

    Hi,

    So I've been working on this program to make tournament matches for beach volley. Now I'm stuck on this one thing I just can't figure out.

    First little info: (I'm Belgian so some words are Dutch)
    -Class Speler: has arraylist with information with which player already has been played (codesLijst)
    -Class Inschrijvingen: makes players sign up with their level and name
    -Class Wedstrijd: makes matches, checks with Inschrijvingen if the players already have played together. If so, gives message that they need to change partner. If not, makes them a team.

    Now if I run the program with fresh players, none of them have had a partner, it works fine. If I make just one new team and the other already exists, it also works fine. It's when I make the two teams with existing records (to see whether they already played together), that the error comes.

    I already made most of the fast enumerations Iterators, but it still isn't working.

    I know it's a lot of code, but the error can come from anywhere I believe.. :/ I "Highlighted" the errors code lines in the code.

    Anyone who can help, I will be very thankful! :)


    ERROR MESSAGE:

    Java Code:
    Nils Inge Jorg Jill
    OK
    OK1 Jorg 1
    OK4
    NOK Jorg Nils
    NNOK Jorg Jorg
    NNOK1
    NNOK2
    NNOK3
    NNOK3
    NNOK4
    NNOK5
    JorgH1JillD2 JoJi2
    JoJi2 Speler 2
    JorgH1JillD2
    JoJi2 Speler 1
    NEW TEAM 2 MADE
    Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
    	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:886)
    	at java.util.ArrayList$Itr.next(ArrayList.java:836)
    	at me.kevoc.beach.Wedstrijden.makePloegske(Wedstrijden.java:230)
    	at me.kevoc.beach.Wedstrijden.makePloeg(Wedstrijden.java:157)
    	at me.kevoc.beach.Wedstrijden.access$13(Wedstrijden.java:132)
    	at me.kevoc.beach.Wedstrijden$EventHandlerAll.actionPerformed(Wedstrijden.java:408)


    Java Code:
    private void makePloeg() { /** HERE IS THE at me.kevoc.beach.Wedstrijden.access$13(Wedstrijden.java:132)*/
    		Iterator<Speler> itSpelers =  inschrijving.getSpelersHeren().iterator();
    		System.out.println("OK");
    		
    		while(itSpelers.hasNext()){
    			ploegSpeler = itSpelers.next();
    			arraySize = ploegSpeler.getCode().size();
    			System.out.println("OK1 " + ploegSpeler.getNaam() + " " + arraySize);
    				
    				if(ploegSpeler.getNaam().equalsIgnoreCase(stringTxtA)){
    					System.out.println("OK2");
    					if(arraySize == 0){
    						makeCode();
    						System.out.println("Makes code 1");
    						
    					}else makePloegske();System.out.println("OK3");
    					
    				}else{
    					
    					if(ploegSpeler.getNaam().equalsIgnoreCase(stringTxtC)){
    						System.out.println("OK4");
    						if(arraySize == 0){
    							makeCode();
    							System.out.println("Makes code 2");
    							
    						}else makePloegske();System.out.println("OK5"); /**HERE IS THE at me.kevoc.beach.Wedstrijden.makePloeg(Wedstrijden.java:157) */
    					}
    				}
    		}
    	}


    Java Code:
    private void makePloegske() {
    		System.out.println("NOK " + ploegSpeler.getNaam() + " " + stringTxtA);
    		
    		Iterator<Speler> itSpelersTmp =  inschrijving.getSpelersDames().iterator();
    		
    		if(ploegSpeler.getNaam().equalsIgnoreCase(stringTxtA)){
    			System.out.println("NOK1");
    			
    			Iterator<String> itStringTmp =  ploegSpeler.getCode().iterator();
    			
    			while(itStringTmp.hasNext()){
    				String tmpString = itStringTmp.next();
    				
    			//for (String s1: ploegSpeler.getCode()){
    				System.out.println("NOK2");
    				
    				while(itSpelersTmp.hasNext()){
    					Speler tmpSpeler = itSpelersTmp.next();
    				
    				//for (Speler sp2: inschrijving.getSpelersDames()){
    					System.out.println("NOK3");
    					if(tmpSpeler.getNaam().equalsIgnoreCase(stringTxtB)){
    						System.out.println("NOK4");
    						
    						Iterator<String> itStringTmp2 =  tmpSpeler.getCode().iterator();
    						
    						while(itStringTmp2.hasNext()){
    							String tmpString2 = itStringTmp2.next();
    						
    						//for (String s2: tmpSpeler.getCode()){
    							System.out.println("NOK5");
    							if(tmpString2.equalsIgnoreCase(tmpString)){
    								//MAKE NEW TEAM PLEAZE
    								System.out.println("MAKE NEW TEAM PLEAZE | MAKE PLOEGSKE | TEAM 1");
    							}else{
    								makeCode();
    								System.out.println("NEW TEAM 1 MADE");
    							}
    						}
    					}
    				}
    			}
    		}else{
    
      /**SAME AS ABOVE, JUST OTHER JTEXTFIELDS*/
    
    			System.out.println("NNOK " + ploegSpeler.getNaam() + " " + stringTxtC);
    		if(ploegSpeler.getNaam().equalsIgnoreCase(stringTxtC)){
    			System.out.println("NNOK1");
    			
    			Iterator<String> itStringTmp =  ploegSpeler.getCode().iterator();
    			
    			while(itStringTmp.hasNext()){
    				String tmpString = itStringTmp.next();
    			
    		//	for (String s1: ploegSpeler.getCode()){
    				System.out.println("NNOK2");
    				
    				while(itSpelersTmp.hasNext()){
    					Speler tmpSpeler = itSpelersTmp.next();
    				
    				//for (Speler sp2: inschrijving.getSpelersDames()){
    					System.out.println("NNOK3");
    					if(tmpSpeler.getNaam().equalsIgnoreCase(stringTxtD)){
    						System.out.println("NNOK4");
    						
    						
    						Iterator<String> itStringTmp2 =  tmpSpeler.getCode().iterator();
    						
    						while(itStringTmp2.hasNext()){
    							String tmpString2 = itStringTmp2.next();  /**HERE IS THE at me.kevoc.beach.Wedstrijden.makePloegske(Wedstrijden.java:230) */
    							
    						//for (String s2: sp2.getCode()){
    							System.out.println("NNOK5");
    							if(tmpString2.equalsIgnoreCase(tmpString)){
    								//MAKE NEW TEAM PLEAZE
    								System.out.println("MAKE NEW TEAM PLEAZE | MAKE PLOEGSKE | TEAM 2");
    							}else{
    								makeCode();
    								System.out.println("NEW TEAM 2 MADE");
    							}
    						}
    					}
    				}
    			}
    		}
    		}
    	}


    Java Code:
    private void makeCode() {
    		
    		//CHECK FOR TXTSPELER1
    		//CODE FOR SEARCH
    		if(ploegSpeler.getNaam().equalsIgnoreCase(stringTxtA)){
    			
    		String tmpFirst2for1 = (stringTxtA).substring(0, 2);
    		String tmpFirst2for2 = (stringTxtB).substring(0, 2);
    		String tmpCode = tmpFirst2for1 + tmpFirst2for2 + wedstrijdNr;
    		//END
    				
    		//CODE FOR COMPARISON
    		String tmpPloeg1 = null;
    		String tmpPloeg2 = null;
    		
    		tmpPloeg1 = ploegSpeler.getPloeg();
    		
    		for (Speler getPloeg: inschrijving.getSpelersDames()){
    			
    			if(getPloeg.getNaam().equalsIgnoreCase(stringTxtB)){
    				tmpPloeg2 = getPloeg.getPloeg();
    			}	
    		}
    		String tmpCodeC = stringTxtA + tmpPloeg1 + stringTxtB + tmpPloeg2;
    		//END	
    		System.out.println( tmpCodeC + " " + tmpCode);
    		
    			Iterator<Speler> itSpelers =  inschrijving.getSpelersDames().iterator();
    			
    			while(itSpelers.hasNext()){
    				Speler sp1 = itSpelers.next();
    				
    				if(sp1.getCode().size() == 0){
    				
    					if(sp1.getNaam().equalsIgnoreCase(stringTxtB)){
    						sp1.setCode(tmpCodeC);
    						codesLijst.add(tmpCode);
    						System.out.println(tmpCode + " Speler 2");
    						System.out.println(tmpCodeC);
    					}
    			}else{
    
    				Iterator<String> itCodes =  ploegSpeler.getCode().iterator();
    				
    				while(itCodes.hasNext()){
    					String c1 = itCodes.next();
    				
    				if(c1.equalsIgnoreCase(tmpCodeC)){
    						System.out.println("MOET ANDER TEAM WORDEN GEMAAKT");
    				}else{
    						
    					if(sp1.getNaam().equalsIgnoreCase(stringTxtB)){
    						sp1.setCode(tmpCodeC);
    						codesLijst.add(tmpCode);
    						System.out.println(tmpCode + " Speler 2");
    						System.out.println(tmpCodeC);
    					}
    				}
    				}
    			}
    		
    		}
    		
    		if(ploegSpeler.getNaam().equalsIgnoreCase(stringTxtA)){
    			ploegSpeler.setCode(tmpCodeC);
    			System.out.println(tmpCode + " Speler 1");
    		}		
    		}else{
    		
    			
    			//------------------------------------------------------------------------------------------
    			
    			//CHECK FOR TXTSPELER3  /**SAME AS ABOVE, JUST OTHER JTEXTFIELDS*/
    			
    			//------------------------------------------------------------------------------------------
    			if(ploegSpeler.getNaam().equalsIgnoreCase(stringTxtC)){
    				
    				String tmpFirst2for1 = (stringTxtC).substring(0, 2);
    				String tmpFirst2for2 = (stringTxtD).substring(0, 2);
    				String tmpCode = tmpFirst2for1 + tmpFirst2for2 + wedstrijdNr;
    				//END
    						
    				//CODE FOR COMPARISON
    				String tmpPloeg1 = null;
    				String tmpPloeg2 = null;
    				
    				tmpPloeg1 = ploegSpeler.getPloeg();
    				
    				for (Speler getPloeg: inschrijving.getSpelersDames()){
    					
    					if(getPloeg.getNaam().equalsIgnoreCase(stringTxtD)){
    						tmpPloeg2 = getPloeg.getPloeg();
    					}	
    				}
    				String tmpCodeC = stringTxtC + tmpPloeg1 + stringTxtD + tmpPloeg2;
    				//END	
    				System.out.println( tmpCodeC + " " + tmpCode);
    				
    					Iterator<Speler> itSpelers =  inschrijving.getSpelersDames().iterator();
    					
    					while(itSpelers.hasNext()){
    						Speler sp1 = itSpelers.next();
    						
    						if(sp1.getCode().size() == 0){
    						
    							if(sp1.getNaam().equalsIgnoreCase(stringTxtD)){
    								sp1.setCode(tmpCodeC);
    								codesLijst.add(tmpCode);
    								System.out.println(tmpCode + " Speler 2");
    								System.out.println(tmpCodeC);
    							}
    					}else{
    
    						Iterator<String> itCodes =  ploegSpeler.getCode().iterator();
    						
    						while(itCodes.hasNext()){
    							String c1 = itCodes.next();
    						
    						if(c1.equalsIgnoreCase(tmpCodeC)){
    								System.out.println("MOET ANDER TEAM WORDEN GEMAAKT");
    						}else{
    								
    							if(sp1.getNaam().equalsIgnoreCase(stringTxtD)){
    								sp1.setCode(tmpCodeC);
    								codesLijst.add(tmpCode);
    								System.out.println(tmpCode + " Speler 2");
    								System.out.println(tmpCodeC);
    							}
    						}
    						}
    					}
    				
    				}
    				
    				if(ploegSpeler.getNaam().equalsIgnoreCase(stringTxtC)){
    					ploegSpeler.setCode(tmpCodeC);
    					System.out.println(tmpCode + " Speler 1");
    				}				
    				}
    		}
    		
    	}

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: Concurrent Mod Exception

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Concurrent Mod Exception

    Concurrent modification can come up if you're trying to edit something that is already in use. For example, if you have
    Java Code:
    for(int i = 0; i < arraylist.size(); i++) {
    System.out.println(arraylist.get(i).toString());
    }
    and in the middle of that looping, your program tries to add or remove something from the array.

    There is a solution where you use something called synchronization. Maybe take a look at this:
    Synchronized Methods (The Java™ Tutorials > Essential Classes > Concurrency)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Concurrent Mod Exception

    Quote Originally Posted by AlexGraal View Post
    Concurrent modification can come up if you're trying to edit something that is already in use. For example, if you have
    Java Code:
    for(int i = 0; i < arraylist.size(); i++) {
    System.out.println(arraylist.get(i).toString());
    }
    and in the middle of that looping, your program tries to add or remove something from the array.

    There is a solution where you use something called synchronization. Maybe take a look at this:
    Synchronized Methods (The Java™ Tutorials > Essential Classes > Concurrency)
    That code won't produce a CME, no matter what happens to 'arraylist' in another thread.
    It's limited to using an Iterator, and changing the underlying Collection the Iterator is using. No need for threads. And synchronisation has no effect on it.

    Java Code:
        public static void main(String[] args) {
            List<String> stringList = new ArrayList<>();
            stringList.add("Something");
            for(String someString : stringList) {
                System.out.println(someString);
                stringList.add("Another string");
            }
        }
    That will produce a CME as 'for(String someString : stringList) ' uses an Iterator<String> under the hood.

    So, for the OP, at some point you are adding or removing something from the array referenced by one of the iterators in the 'makePloegske' method. I can't tell which one as it's not clear which line the exception is being thrown from.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Praijer is offline Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Re: Concurrent Mod Exception

    Quote Originally Posted by AlexGraal View Post
    Concurrent modification can come up if you're trying to edit something that is already in use. For example, if you have
    Java Code:
    for(int i = 0; i < arraylist.size(); i++) {
    System.out.println(arraylist.get(i).toString());
    }
    and in the middle of that looping, your program tries to add or remove something from the array.

    There is a solution where you use something called synchronization. Maybe take a look at this:
    Synchronized Methods (The Java™ Tutorials > Essential Classes > Concurrency)

    Quote Originally Posted by Tolls View Post
    That code won't produce a CME, no matter what happens to 'arraylist' in another thread.
    It's limited to using an Iterator, and changing the underlying Collection the Iterator is using. No need for threads. And synchronisation has no effect on it.

    Java Code:
        public static void main(String[] args) {
            List<String> stringList = new ArrayList<>();
            stringList.add("Something");
            for(String someString : stringList) {
                System.out.println(someString);
                stringList.add("Another string");
            }
        }
    That will produce a CME as 'for(String someString : stringList) ' uses an Iterator<String> under the hood.

    So, for the OP, at some point you are adding or removing something from the array referenced by one of the iterators in the 'makePloegske' method. I can't tell which one as it's not clear which line the exception is being thrown from.

    Thanks both for your comments!

    I was aware of what the errors meant, but I was unable to solve it, no matter what I did. So I decided to completely rewrite the code, which has solved to problem.
    It's much cleaner and better code now, so that got things going for me :D

    Cheers!

  6. #6
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Concurrent Mod Exception

    Oh! Interesting. I didn't think about that at all. Yes, you're right. When I went to my professor with the same problem and got that solution, I had that kind of iteration. I didn't realize. Thanks

Similar Threads

  1. Concurrent modification exception while using iterator
    By Miromancer in forum Advanced Java
    Replies: 5
    Last Post: 09-25-2012, 01:28 PM
  2. Replies: 1
    Last Post: 12-01-2011, 01:22 PM
  3. Replies: 0
    Last Post: 11-30-2011, 04:20 PM
  4. concurrent programming
    By abdullahansari in forum New To Java
    Replies: 1
    Last Post: 12-02-2010, 07:33 PM
  5. help with concurrent modification exception
    By jdgallag in forum New To Java
    Replies: 1
    Last Post: 11-30-2008, 09:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •