java.lang.NullPointerException getting while adding '{PAGE \* Arabic \*MERGEFORMAT }' of {NUMPAGES \* Arabic \*MERGEFORMAT}' in word document

Getting error java.lang.NullPointerException getting while adding {PAGE \* Arabic \*MERGEFORMAT } of {NUMPAGES \* Arabic \*MERGEFORMAT} in word document.

Sample code :

try
{
    Locale current = CurrentThreadSettings.getLocale();

    CurrentThreadSettings.setLocale(Locale.forLanguageTag("en-US"));

    doc = new Document(strPath + "Credit Memo Template.docx");

    doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);

    merge = doc.getMailMerge();
    // Reading the data from xml file for the mail merge
    DataSet dataSet = new DataSet();
    dataSet.readXml(strPath + "inputxml.xml");


    int cleanupOptions = MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS |
    MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS |
    MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS;

    doc.getMailMerge().setCleanupOptions(cleanupOptions);
    doc.getMailMerge().executeWithRegions(dataSet);


    // merge the header fields
    DataTableCollection tables = dataSet.getTables();
    if (tables != null && tables.getCount() > 0)
    {
        merge.execute(tables.get("PrintForm"));
    }

    // Remove blank rows.
    // In the case of the Aspose Word 22.12 Jar, the issue can be reproduced while rw.remove() called in this method
    removeBlankRows(doc);


    com.aspose.words.PdfSaveOptions saveOption = new com.aspose.words.PdfSaveOptions();
    com.aspose.words.DownsampleOptions downSampleOption = new DownsampleOptions();
    downSampleOption.setDownsampleImages(false);
    saveOption.setDownsampleOptions(downSampleOption);

    doc.getMailMerge().deleteFields();
    doc.updateFields(); //Aspose-words-22.12.jar causes java.lang.NullPointerException while using {PAGE \* MERGEFORMAT} {NUMPAGES \* MERGEFORMAT}
    doc.save(strPath + "OutPutFile_de_AT26.pdf", saveOption);

    CurrentThreadSettings.setLocale(current);


}
catch (Exception e)
{
    System.out.println(e.getMessage());
    e.printStackTrace();
    throw new MergeException(e.getMessage());
}

Word Template file and input xml file has been attached.

ToForum.zip (20.8 KB)

@akondewar Unfortunately I cannot reproduce the problem on my side neither using 22.12 nor using the latest 24.1 version of Aspose.Words. But on my side I do not have method removeBlankRows. Could you please provide this method, maybe it causes the problem on your side.

Hi,
Here is the method

void removeBlankRows ( Document doc ) {
	try {
        DocumentBuilder builder = new DocumentBuilder(doc);
		NodeCollection<?> coll = doc.getChildNodes(NodeType.TABLE, true);
		for(Object obj : coll) {
			Table t = (Table) obj;
			RowCollection rows = t.getRows() ;
			for ( Object row : rows) {
				Row rw = (Row) row ;
				if ( rw != null ) {
					if ( rw.toString(SaveFormat.TEXT).trim().isEmpty() ) {
                        rw.remove();
                    }
				}
			}
		}
	}catch (Exception e){
		System.out.println( e.getMessage() );
	}
}

previous Reference :
https://forum.aspose.com/t/getting-error-java-lang-nullpointerexception-page-mergeformat-numpages-mergeformat-tag-in-word-file/265588/3

@akondewar Thank you for additional information. Unfortunately, the problem is still not reproducible on my side. Here is PDF document produced by your code on your side: OutPutFile_de_AT26.pdf (38.0 KB)

@alexey.noskov
Thanks, we are getting error.

Hi we are using 22.12 aspose jar, please let us know the jar version.

Error log, source code, template in input xml file has been attached
ToForum.zip (24.3 KB)

@akondewar I have tested with both 24.1 and 22.12 version of Aspose.Words and in both case the problem is not reproducible on my side.

Hi @alexey.noskov
Still we are getting error.
I have attached the sample code, kindly check
Error log, source code, template in input xml file
ToForum.zip (24.3 KB)

@akondewar Thank you for additional information. The problem is caused by doc.updateFields() which is called after executing mail merge. This this not required since upon executing mail merge fields are updated internally.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-26440

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi @alexey.noskov
Thanks for response.

doc.updateFields() was suggested in following issue.

https://forum.aspose.com/t/java-lang-nullpointerexception-while-using-page-mergeformat-numpages-mergeformat-tag-in-word-template-file-when-row-remove-is-called-before-document-save/265912/4

@akondewar Call the doc.updateFields() method after calling removeBlankRows or better before saving the document to PDF.

@alexey.noskov doc.updateFields() already called before saving the document to PDF.
You can check in my provide code.
https://forum.aspose.com/t/java-lang-nullpointerexception-getting-while-adding-page-arabic-mergeformat-of-numpages-arabic-mergeformat-in-word-document/277285/7?u=akondewar

@akondewar Yes, but the method is also called before executing removeBlankRows. So remove the line where doc.updateFields() is called before removeBlankRows method.