Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Error: java.util.ConcurrentModificationException

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Error: java.util.ConcurrentModificationException

    Yesterday I hoped I had finished program 1, but it appears to have a fault in it.
    And that is that it won't take empty cells.
    (It is a program that read in an excel file to a MultiHashMap, uses Apache POI)
    So know I have made this:

    	public void OpenReadFileTwo() {
    		// Open JFileChooser. 
    		chooser2.showOpenDialog(null); 
    		// Get the selected file. 
    		java.io.File file2 = chooser2.getSelectedFile();
    		// Create a FileInputStream. 
    		FileInputStream fileInput2 = null; 
    		//Get the name of the selected file. 
    		fileName2= file2.getName();
    		// Create a FileInputStream that will be used to read excel files. 
    		try {
    			fileInput2 = new FileInputStream(file2);
    			// Create an excel workbook. 
    			XSSFWorkbook workbook = new XSSFWorkbook(fileInput2); 
    			// Get the first sheet. 
    			XSSFSheet fileSheet2 = workbook.getSheetAt(0);
    			// Create Iterator. 
    			Iterator<Row> rows2 = fileSheet2.rowIterator();
     
    			while (rows2.hasNext()) {
    				//XSSF = .xlsx files, HSSF = .xls files. 
    				XSSFRow row = (XSSFRow) rows2.next();
    				// Get the empty cells in between as blank. 
    				//row.getCell(blank, Row.CREATE_NULL_AS_BLANK);
    				//System.out.println("Blank: " + row.getCell(blank, Row.CREATE_NULL_AS_BLANK));
     
    				// Iterate the row. 
    				Iterator<Cell> cells2 = row.cellIterator();
    				// Count every time +1. 
    				counterFile2 = counterFile2 +1; 
    				//Get only the first column in the excel file, column A. 
    				columnNumbers2 = "A" +counterFile2; 
    				CellReference cellReference2 = new CellReference(columnNumbers2);
    				Cell cellDataFirstColumn2 = row.getCell(cellReference2.getCol()); 
    				// Change cellDataFirstColumn2 to a String format. 
    				cellDataFirstColumn2String = "" + cellDataFirstColumn2; 
    				// Store the first column data in an arrayList. 
    				ArrayListFileData2.add(cellDataFirstColumn2String);
     
    				while (cells2.hasNext()) {
    					//
    					XSSFCell cell2 = (XSSFCell) cells2.next();
    					//
    					row.getCell(blank, Row.CREATE_NULL_AS_BLANK);
    					//System.out.println("Blank?: " + row.getCell(blank, Row.CREATE_NULL_AS_BLANK));
    					String BlankOrNot = ("" + row.getCell(blank, Row.CREATE_NULL_AS_BLANK));; 
    					//
    					if (BlankOrNot.length() != 0) {
    						System.out.println("Fish");
    						//continue;
    						Object cell2Object = "" + cell2;
    						//
    						mapFile2.put(cellDataFirstColumn2String, cell2Object);
    						System.out.println("cell2Object: " + cell2Object);
    					}
    					else {
    						System.out.println("Salmon");
    						//
    						Object blankeCell = " "; 
    						mapFile2.put(cellDataFirstColumn2String, blankeCell);
    						System.out.println("cell2Object: " + blankeCell);
    					}
     
    					blank = blank +1; 					
    					System.out.println(blank);
     
     
    				}
    				blank = 0; 
    			} counterFile2++; 
    		} catch (IOException e2) {
    			JOptionPane.showMessageDialog(null, "No file 2 selected, or the file is not a .xlsx file!");
    		} finally {
    			// Show the user the name of the selected file that was loaded in.
    			label2.setText(" File selected: " + fileName2); 
    			label2.setForeground(Color.BLUE);
    		}
    	}

    And it looks like it is working, because it now grabs the empty cell and prints " " on my screen.
    But after an empty cell have been found he runs to the catch.
    And I looked up this error, and it says something is in use that I am not allowed to change.
    But I don't see what is in use....

    Also it only runs to the catch when an empty cell is found.
    So if goes well, but else not.

    And I don't understand why. -_-


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: java.util.ConcurrentModificationException

    this error, and it says something is in use that I am not allowed to change.
    Can you post the full contents of the error message here?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Error: java.util.ConcurrentModificationException

    Sure.
    This is the error code I am having:

    Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
    	at java.util.TreeMap$PrivateEntryIterator.nextEntry(Unknown Source)
    	at java.util.TreeMap$ValueIterator.next(Unknown Source)
    	at SecondMatcherGridLayout.OpenReadFileTwo(SecondMatcherGridLayout.java:359)
    	at SecondMatcherGridLayout.actionPerformed(SecondMatcherGridLayout.java:198)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

    Line 359 is:
    XSSFCell cell2 = (XSSFCell) cells2.next();

    What is does now is:
    - It looks if there are next cells.
    - And write the information from those cells away to a MultiHashMap.
    - If there is a cell with nothing in between put a " " in the MultiHashMap.
    - And after that add the next cell to MultiHashmap.

    It all goes into the MultiHashMap, and if I let it print it does it the right way.
    But after that it throws this error. (After there is an empty cell found.)
    Last edited by Purple01; September 28th, 2012 at 06:16 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: java.util.ConcurrentModificationException

    Where does the code make any changes to the collection while it is looking at it with an iterator?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Error: java.util.ConcurrentModificationException

    That is a bit of the problem.
    It only runs this subclass.
    So it is not used somewhere else yet.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: java.util.ConcurrentModificationException

    Where is the collection modified while iterating over it? A reason for the error from the API doc:
    If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Error: java.util.ConcurrentModificationException

    I think it is this one:
    (When I remove this one I don't get the error.)

    String BlankOrNot = ("" + row.getCell(blank, Row.CREATE_NULL_AS_BLANK));
    But that is some code I need to find empty cells in between.

    I found that code here:
    java - How to get an Excel Blank Cell Value in Apache POI? - Stack Overflow

    And I think that was the right way to implement it in my code.
    Did I maybe made a mistake here?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: java.util.ConcurrentModificationException

    It's strange that a method named getCell makes changes to the object.
    What is the definition for that method?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Error: java.util.ConcurrentModificationException

    There is not so much to be found on this function.
    It only should make sure that empty cells are not skipped over.
    And nothing else.

  10. #10
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Error: java.util.ConcurrentModificationException

    Maybe it helps if I show some output this makes?

    This is my excel file:
    Sample_4 Cy3 Cat Old
    Sample_7 <empty cell here> Human Old

    This is what is hapening when it runs:
    {Sample_4=[Sample_4, Cy3, Cat]}

    {Sample_4=[Sample_4, Cy3, Cat, Old]}

    {Sample_4=[Sample_4, Cy3, Cat, Old] Sample_7=[Sample_7]}

    {Sample_4=[Sample_4, Cy3, Cat, Old] Sample_7=[Sample_7,  , Human]}

    And then this exception shows up:
    Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException

    ###


    I so hope I can fix this problem today, before the weekend starts. :p
    Last edited by Purple01; September 28th, 2012 at 02:18 AM.

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error: java.util.ConcurrentModificationException

    Quote Originally Posted by Purple01 View Post
    Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
    I would suggest you utilize your favorite search engine on java.util.ConcurrentModificationException so you understand what is going on.
    Then if you have questions please post your code with the question. Just a little fyi, it is much easier to follow if you past more code that shows what you are trying to do rather than a bunch of snippets and comments about them.

    Good luck with the exception, once you understand the exception you may be able to fix the cause of the problem.

  12. #12
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Error: java.util.ConcurrentModificationException

    I solved it.
    I have put that line of code a few lines above and I made the line Return something.
    Now it does work.

    Thanks everyone.

Similar Threads

  1. java.lang.String cannot be cast to java.util.Hashtable
    By ashin12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 1st, 2012, 06:17 AM
  2. Replies: 4
    Last Post: February 17th, 2012, 07:05 PM
  3. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  4. java.util.ConcurrentModificationException Issue
    By joshuaa in forum Collections and Generics
    Replies: 1
    Last Post: December 2nd, 2010, 04:41 PM
  5. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM