We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Error occurred while converting FIELD_TOC_ENTRY field to static text

I have a document (Text.zip (15.2 KB)) obtained from third-party source.
I need to convert hidden field {TC "1.1 Test \fC \1 “2”} into a static text. (I use compare function in further processing)
But on line FieldsHelper.ConvertFieldsToStaticText(doc, targetFieldType); I get an exception ‘java.lang.NullPointerException’

Document doc = new Document(“Test.docx”);
FieldsHelper.ConvertFieldsToStaticText(doc, targetFieldType);

The function ConvertFieldsToStaticText I’ve found in documentation on this page:

I’ve tried to add try…catch like this:
try {
FieldsHelper.ConvertFieldsToStaticText(doc, targetFieldType);
} catch (Exception ex) {}
but resulted document contains the text ‘Test’ only, and there isn’t text '. This is text. ’ in continue.


Thanks for your inquiry. A field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. Your document contains the field FIELD_TOC_ENTRY and it does not have field separator. The FieldsHelper.ConvertFieldsToStaticText method extracts the text between field separator and field end. This is the reason you are getting the exception.

We suggest you please use Field.Unlink method instead of FieldsHelper.ConvertFieldsToStaticText. Please use Field.Remove method for FIELD_TOC_ENTRY field. Hope this helps you.