Following is the code which is while iterating adding a new paragraph before a specific paragraph.
public void resolveBodyElements1(List<IBodyElement> elements) {
for(IBodyElement element : elements) {
if(PARAGRAPH.equalsIgnoreCase(element.getElementType().name())) {
XWPFParagraph paragraph = (XWPFParagraph)element;
if(paragraph.getText().equalsIgnoreCase("[{multiline-bulletpoints-markdown}]")) {
XmlCursor paragraphCursor = paragraph.getCTP().newCursor();
XWPFParagraph paragraph1 = paragraph.getDocument().insertNewParagraph(paragraphCursor);
paragraph1.createRun().setText("This is a test paragraph inserted.");
}
System.out.println(paragraph.getText());
}
}
}
But it ends up in a ConcurrentModification Exception, However, I am using a for loop instead of an iterator or a foreach loop. Can anyone please guide me in this regard to add a paragraph in a document while iterating the paragraphs and avoid this ConcurrentModification Exception?