Bug 68358 - XWPFTable.addRow places copy of XWPFTableRow CTRow in XWPFTable CTTbl
Summary: XWPFTable.addRow places copy of XWPFTableRow CTRow in XWPFTable CTTbl
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: XWPF (show other bugs)
Version: 5.2.3-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2023-12-18 11:22 UTC by Mikhail
Modified: 2024-02-25 12:38 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Mikhail 2023-12-18 11:22:32 UTC
Here is sample code, which copy first row of table and populate it with some text

    public static void sample(XWPFTable table) {
        final CTRow templateRow = (CTRow) table.getRow(0).getCtRow().copy();
        final XWPFTableRow tableRow = new XWPFTableRow((CTRow) templateRow.copy(), table);
        tableRow.getCell(0).setText("Sample text"); // OK
        table.addRow(tableRow); // <-- cloned CTRow is copying
        tableRow.getCell(0).setText("Another sample text"); // <-- Modifying old CTRow  
        // After writing document row still contain "Sample text"

The problem is in method XWPFTable.addRow

   public void addRow(XWPFTableRow row) {
        ctTbl.setTrArray(getNumberOfRows() - 1, row.getCtRow()); // <-- copy content of row CTRow
        tableRows.add(row); // <-- XWPFTableRow still points to own row.getCtRow() and subsequent calls not affected final document