java.lang.NullPointerException while using{PAGE \*MERGEFORMAT} {NUMPAGES \*MERGEFORMAT} tag in word template file when Row.remove() is called before document.save ()

Aspose-words-22.12.jar causes java.lang.NullPointerException while using {PAGE \* MERGEFORMAT} {NUMPAGES \* MERGEFORMAT} tags in word template files, when Row.remove() is called before document.save().

It is working fine with ‘aspose-words-15.11.jar’

Error:

java.lang.NullPointerException
at com.aspose.words.zzXur.zzYeB(Unknown Source)
at com.aspose.words.zzXur.zzab(Unknown Source)
at com.aspose.words.zzVSs.zzzD(Unknown Source)
at com.aspose.words.zzWL.zzab(Unknown Source)
at com.aspose.words.zzXXz.zzab(Unknown Source)
at com.aspose.words.zzY9j.zzab(Unknown Source)
at com.aspose.words.zzZLe.zzab(Unknown Source)
at com.aspose.words.zzVSs.zzWpk(Unknown Source)
at com.aspose.words.zzVSs.zzab(Unknown Source)
at com.aspose.words.Document.zzab(Unknown Source)
at com.aspose.words.zzWDc.zzYCO(Unknown Source)
at com.aspose.words.zzWDc.zzab(Unknown Source)
at com.aspose.words.zzWK2.zzab(Unknown Source)
at com.aspose.words.Document.zzYCO(Unknown Source)
at com.aspose.words.Document.zzab(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at com.aspose.words.Document.save(Unknown Source) 

Sample Code:

try
{
    merge.executeWithRegions(dataSet);

    doc.updateFields();

    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();
                }
            }
        }
    }
    doc.save(strPath + "testoutputdoc.pdf");
}
catch (Exception e)
{
    System.out.println(e.getMessage());
}

@akondewar Could you please attach your input document here for testing? We will check the issue and provide you more information.

@alexey.noskov
Please find the attched input document Sales Order Invoice Template.docx (22.6 KB)

@akondewar
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-25486

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.

You can work the problem around by updating fields before saving the document to PDF:

// ......
doc.updateFields();
doc.save(strPath + "testoutputdoc.pdf");

@akondewar We have completed analysis of the issue. The problem occurs because layout mode is not rebuilt after document model manipulation.

First, UpdateFields() builds the layout model in order to update NumPages field in the header.
After that, the code changes the document model by removing a table row.
The code does not attempt to update document layout after manipulating the document model.

Aspose.Words does not detect document model changes or re-builds the layout model automatically, by design. If a layout model exists, it is used for saving to fixed-page formats. Layout model is supposed to be updated explicitly after all document manipulations are done. So we are going to close the issue as Not a Bug.

The issues you have found earlier (filed as WORDSNET-25486) have been fixed in this Aspose.Words for Java 23.7 update.