Can't remove the first of a table

Hi, I am getting this error when I use row.remove() on the first row in a table.

java.lang.IllegalStateException: Cannot remove because there is no parent.
at com.aspose.words.Node.remove(Unknown Source) ~[aspose-words-19.4.jar:19.4]
at com.manulife.pension.pinpointpricingclient.vo.DeleteNullRows.nodeInserted(DeleteNullRows.java:29) ~[classes/:na]

DeleteNullRows is below:

@Slf4j
public class DeleteNullRows implements INodeChangingCallback {
@Override
public void nodeInserted(NodeChangingArgs arg0) throws Exception {
Node node = arg0.getNode();

if (node.getNodeType() == NodeType.RUN) {
Run run = (Run) node;
if (run.getRange().getText().contains(“DELETE”)) {
Row row = (Row) run.getAncestor(NodeType.ROW);
if (row != null) {
log.info("Row info:: " + row.getCells().toArray()[0].getText());
row.remove();
}
}
}
}

@ganeshtech,

I think, inside INodeChangingCallback, you can insert a bookmark at the place whose row you want to delete. And later when table is fully constructed (after builder.endTable), you can find the ancestor row of BookmarkStart and remove it. Hope, this helps.

1 Like