Getting Error 'Cannot remove because there is no parent.'

Hi,

  • We are encountering the issue when uploading the template. Getting below error when trying to upload and preview the template
    Caused by: java.lang.IllegalStateException: Cannot remove because there is no parent. at com.aspose.words.Node.remove(Unknown Source)

  • When I debug the API code, it is calling remove() method in Node.class that is Aspose code. Now checking whether the getParentNode() is NULL, if it is NULL then throw the error.

  • Sample Template: Sample Template.docx (36.8 KB)

We would like to ask you what changed or what we do wrong/ need to change in order to make it work. We would like to have some workaround instead of changing it in every template. And how to fix this from the template standpoint.

Thank you.

@jcash_casenetllc_com, could you please provide all Aspose.Words API calls occuring before the remove() method? If possible, please extract the code working with the template into a minimal console application that can reproduce the problem with the template you shared. This will help us analyze your problem and provide a workaround.

@denis.shvydkiy, As mentioned in the previous comment, I extracted the code working with the template into a minimal console application and able to reproduce the problem with the template.

Sample console application: Aspose.zip (226.8 KB)

Template: Sample Template.docx (36.8 KB)

Error screenshot:

Instruction to run the sample console application:

  • Go to the Main.java, verify the ‘fileTemplate’ path of the template

  • Run the Main.main().

@jcash_casenetllc_com The problem occurs because the same paragraph is added into compositeNodes collection twice. So later in cleanupNodesParagraphs method the code tries to remove already removed paragraph. You can avoid this by modifying your code like this:

if(!compositeNodes.contains(curNode))
    compositeNodes.add(curNode);

@alexey.noskov, Thanks for the update and this helps us to fix the issue from code standpoint.
One more question from our side, until the code is fix Is it possible to have a workaround from the template standpoint.

Thank you.

1 Like

@jcash_casenetllc_com I am afraid I do not see a way to fix the problem without modifying the code.