Bug 51148

Summary: XWPFDocument.removeBodyElement(int pos) error
Product: POI Reporter: ptorrodellas
Component: XWPFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: major    
Priority: P2    
Version: 3.7-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description ptorrodellas 2011-05-04 09:40:04 UTC
XWPFDocument.removeBodyElement(int pos) throws IndexOutOfBoundsException whatever the value of the element index.

I think that this is caused by two XWPFDocument class code errors:

1 - In removeBodyElement(int pos), the statement:

bodyElements.remove(pos);

should be placed AFTER the statements:

Integer tablePos = getTablePos(pos); 

and

Integer paraPos = getParagraphPos(pos);

instead of before, otherwise the element is not there in the bodyElements List to be used to locate it in the paragraphs or tables Lists.

2 - In getParagraphPos(int pos) and getTablePos(int pos), the for loop should be:
for(int i = startPos; i >= 0; i--) {
....
}
Comment 1 Nick Burch 2011-05-06 02:14:48 UTC
Good spot. I've cleaned up the code a bit (the table/paragraph code is now generic), and added a bunch of unit tests for it. Fixed in r1100027.