Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Apr 24, 2020 12:44 pm

Hi
When attempting append text in table I'm getting a ConcurrentModificationException
Is possible to update a cell while iterating over objects?

Thanks,
Shane

Code: Select all
for (c = 0; c <= tablecols; c++) {
         for (Object object : table.getRows().get(table.getLastRow().getRowIndex()).getCells().get(c).getParagraphs().get(0)
               .getChildObjects()) {
            if (object instanceof MergeField) {
               MergeField field = (MergeField) object;
               if (field.getText().trim().equalsIgnoreCase(SUM)) {
                  table.getRows().get(table.getLastRow().getRowIndex()).getCells().get(c).getParagraphs().get(0)
                        .appendText(VALUE);
               }
            }
         }
      }

shanerussell86
 
Posts: 5
Joined: Wed Mar 11, 2020 4:05 pm

Fri Apr 24, 2020 2:09 pm

Figured it out to take the position in the table and update after the for loop in case anyone comes across this problem

Code: Select all
for (c = 0; c <= tablecols; c++) {
         for (Object object : table.getRows().get(table.getLastRow().getRowIndex()).getCells().get(c).getParagraphs().get(0)
               .getChildObjects()) {
            System.out.println(object);
            if (object instanceof TextRange) {
               TextRange field = (TextRange) object;
               if (field.getText().trim().equalsIgnoreCase(SUM)) {
                  column = c;
                  row = table.getLastRow().getRowIndex();
               }
            }
         }
      }
      if (row != 0 && column != 0) {
         TableCell cell2 = table.getRows().get(row).getCells().get(column);
         Paragraph p2 = cell2.getParagraphs().get(0);
         p2.getItems().clear();
         p2.appendText(VALUE);
      }

shanerussell86
 
Posts: 5
Joined: Wed Mar 11, 2020 4:05 pm

Sun Apr 26, 2020 1:20 am

Hello,

Glad to hear that you solved this issue yourself and thanks for sharing your workaround.
If you encounter any question related to our product in the future, just feel free to contact us.
Wish you all the best!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc

cron