0

I am trying to print a string in a specific column of a xlsx file using apache poi in java. I have parsed the column x using regex and printing the required string in column y. However the getRowNum() of the code flags ConcurrentModificationExceptio. Below is the code snippet. Please help

|Column X|Column Y|

|Barath  |Bar     |         //Regex Matches Bar and prints it in Column y (Required output).


while (rows.hasNext())
    {
        row=(XSSFRow) rows.next();
        Iterator cells = row.cellIterator();
        while (cells.hasNext())
        {
            cell=(XSSFCell) cells.next();
            if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
            {
                Matcher m = p.matcher(cell.getRichStringCellValue().getString());
                //rownr = row.getRowNum();
                if(m.find()){

                    sheet.createRow(row.getRowNum()).createCell(colnr+6).setCellValue(row.getCell(colnr+4).toString().substring(m.start(),m.end()-1).replace(":", ""));                     //row.getRowNum() function causes Concurrent modification exception  colnr+6 is destination column and Colnr+4 is column to be parsed.
                    System.out.print(row.getCell(colnr+4).toString().substring(m.start(),m.end()-1).replace(":", "")+"\n");          //Prints the parsed string in the console correctly
                }                   
            }
        }
    }
Stephen King
  • 581
  • 5
  • 18
  • 31
  • 1
    `sheet.createRow(row.getRowNum())` means: Create a new row in the `sheet` using the row number of the `row` you have already and from which the same code line has to read cells from. Why? You have the `row` already. There is no need creating it new. – Axel Richter May 16 '17 at 09:42
  • `sheet.createRow(row.getRowNum())` this looks like you want to create a row based on an already existing row. – XtremeBaumer May 16 '17 at 09:43
  • Btw.: code lines like `row.createCell(colnr+6).setCellValue(row.getCell(colnr+4).toString().substring(m.start(),m.end()-1).replace(":", ""));` are reason for termination in some companies. They are not "cool" but simply bad readable. – Axel Richter May 16 '17 at 09:53
  • I tried row.createCell(), however it dint work. I still have the same exception. – Bharath Surendra May 16 '17 at 11:04
  • No, not the same error but a similar. Because you are creating a new cell in the row while you are iterating over the cells in that row. – Axel Richter May 16 '17 at 12:37
  • So, what is the ideal way to overcome that exception. Even after trying lines row.createCell(colnr+6).setCellValue(row.getCell(colnr+4).to‌​String().substring(m‌​.start(),m.end()-1).‌​replace(":", ""));. Please help – Bharath Surendra May 17 '17 at 02:42
  • There is no ideal way predictable from the informations we have. But you must create the cell in the row outside the iteration process over all cells in that row. For example create the cell as blank cell after you got the row but before iterating over the cells in that row. Then within the iteration only **get** the cell again and set the value to it. – Axel Richter May 17 '17 at 03:49

1 Answers1

0

It seems that, when you are creating a row, the original rows collection is modified, while you are iterating over it. Hence the ConcurrentModificationException.

There are a few ways to fix this, but in this case, I don't think you need to create the row at all, since the row already exists. So instead of...

sheet.createRow(...).createCell(colnr+6)

...you would have:

row.createCell(colnr+6)
Torious
  • 3,364
  • 17
  • 24