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

Free Support Forum - aspose.com

Doc.save() fails with null pointer exception while saving a pdf

Hi,
we have an issue where we are editing tables and saving the document as pdf. and it is failing with null pointer exception and a suppressed exception UnmodifiableRandomAccessList. while saving to pdfa it works fine. Can you suggest something that works.
Thanks,
Sindhu

@sindhuV

Could you please ZIP and attach your input Word document here for testing? We will investigate the issue and provide you more information it.

Hi @tahir.manzoor
I am generating the the document using the xml and template. This is the file i got while debugging.pp.zip (148 Bytes)

@tahir.manzoor, The thing is if i save the file as docx file and then again convert it to pdf it works. but directly saving it fails.

This is stacktrace i found
java.lang.NullPointerException
at com.aspose.words.zzYXR.zzO(Unknown Source)
at com.aspose.words.zzYXR.zzZ(Unknown Source)
at com.aspose.words.zzZLB.zzX(Unknown Source)
at com.aspose.words.zzZKC.zzZ(Unknown Source)
at com.aspose.words.zzZL1.zzZ(Unknown Source)
at com.aspose.words.zzZKY.zzZ(Unknown Source)
at com.aspose.words.zzZLB.zzX(Unknown Source)
at com.aspose.words.zzZLB.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.zzZZO.zzY(Unknown Source)
at com.aspose.words.zzZZO.zzZ(Unknown Source)
at com.aspose.words.zzZF1.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)

@sindhuV

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word and XML documents.
  • Please create a simple application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

testfiles.zip (27.7 KB)
Hi @tahir.manzoor,
I made sample java application which takes in word document and add the last row.

BarcodeGen.zip (1.2 KB)
I made some changes according to our code

@sindhuV Thank you for additional information. However, I cannot reproduce the mentioned exception on document save using the latest 21.11.0 version of Aspose.Words for Java.
Though there are catched exceptions on builder.insertField(" =SUM(ABOVE) ") line. This occurs because you are trying to insert field into the cloned row, which is not inserted into the document yet. You can fix this by appending the row to the table right after cloning. Please see the following code:

Row clonedRow = (Row) lastRow.deepClone(true);
table.appendChild(clonedRow);

Please see the modified code:

Document doc = new Document("C:\\Temp\\totalSUmmary.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
LayoutCollector layout = new LayoutCollector(doc);
Row lastRow = table.getLastRow();
Row clonedRow = (Row)lastRow.deepClone(true);
table.appendChild(clonedRow);
CellCollection cells = clonedRow.getCells();
for (int i = 0; i < clonedRow.getCells().getCount(); i++)
{
    try
    {
        String s = cells.get(i).getText().trim();
        if (cells.get(i).toString(SaveFormat.TEXT).trim().equals("SummaryColumn"))
        {
            if (cells.get(i).getFirstParagraph().getFirstChild() != null)
            {
                cells.get(i).getFirstParagraph().getFirstChild().remove();
                if (cells.get(i).getFirstParagraph().getFirstChild() != null)
                {
                    cells.get(i).getLastParagraph().getLastChild().remove();
                }
                layout.clear();
                try
                {
                    doc.updatePageLayout();
                }
                catch (Exception e)
                {
                    System.out.println("error");
                }
            }
            builder.moveTo(cells.get(i).getFirstParagraph());
            Field field = builder.insertField(" =SUM(ABOVE) ");
            field.update();
            double result = Double.parseDouble(field.getResult());
            double fieldResult;
            fieldResult = Math.round(result);

            ((Run)cells.get(i).getFirstParagraph().getChildNodes(NodeType.RUN, true).get(1))
                    .setText(String.valueOf(fieldResult));

            layout.clear();
            try
            {
                doc.updatePageLayout();
            }
            catch (Exception e)
            {
                System.out.println("error");
            }
        }
    }
    catch (Exception e)
    {
        System.out.println("error");
    }
}

layout.clear();
try
{
    doc.updatePageLayout();
}
catch (Exception e)
{
    System.out.println("error");
}
SaveOptions options;
com.aspose.words.PdfSaveOptions pdfOptions = new com.aspose.words.PdfSaveOptions();
pdfOptions.setFontEmbeddingMode(PdfFontEmbeddingMode.EMBED_NONE);
options = pdfOptions;
ByteArrayOutputStream output = new ByteArrayOutputStream();

try
{
    doc.save(output, options);
    byte[] bytes = output.toByteArray();

}
catch (Exception e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("saved");

Hi @alexey.noskov, can you tell me what’s the difference between appendChild and insertAfter. In our case it fails for insertAfter and works for appendChild

@sindhuV appendChild method simply add the specified node as the last child of the composite node. insertAfter method inserts the specified node immediately after the specified reference node. If the composite node is empty, you cannot use insertAfter or insertbefore methods because there is no reference node.

@alexey.noskov but if composite parent is not null then it should work the same way right?

@sindhuV Both methods adds nodes into the composite node. To demonstrate the difference I created a simple code for you:

Document doc = new Document();

// Get the first paragraph of the document. This is a composite node where we will add child nodes.
Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph();

// Now create 3 Run nodes, two of them we will add using appendChild method and one using insertAfter.
Run run1 = new Run(doc, "First");
run1.getFont().setColor(Color.BLUE);
Run run2 = new Run(doc, "Second");
run2.getFont().setColor(Color.RED);
Run run3 = new Run(doc, "Third");
run3.getFont().setColor(Color.GREEN);

// Add the first and the third runs using appendChild.
paragraph.appendChild(run1);
paragraph.appendChild(run3);

// Now we need to add a node (the second run) between the first and the third one.
// Use insertAfter method.
paragraph.insertAfter(run2, run1);

doc.save("C:\\Temp\\out.docx");

@alexey.noskov I understood it. Thank you

Hi @alexey.noskov, I can’t use appendChild in my code. I have to use the insertBefore() or insertAfter() methods because I am adding the rows while populating the table. But somehow when I use these methods it fails while saving a pdf.

@sindhuV Could you please create a simple application or provide code that will allow us to reproduce the problem? We will check your code and provide you more information.

Hi @alexey.noskov,
I have added the sample code, and expected output and input document.
Thank you,
Sindhu
Template.zip (65.7 KB)

@sindhuV Here is the updated code:

Document doc = null;
try
{
    doc = new Document("C:\\Temp\\template2.docx");
}
catch (Exception e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
Row lastRow = table.getLastRow();
Row firstRow = table.getFirstRow();
Row summaryRow = (Row)lastRow.deepClone(true);
Row updatedSummaryRow = (Row)firstRow.deepClone(true);
int startPage = 0;
LayoutCollector layout = new LayoutCollector(doc);
try
{
    startPage = layout.getStartPageIndex(table);
}
catch (Exception e1)
{
    e1.printStackTrace();
}

createSummaryRow(summaryRow, updatedSummaryRow, doc);
double[] floatingFirstRowSum = new double[summaryRow.getCells().getCount()];

clearAndUpdatePageLayout(doc, layout);
table.getLastRow().remove();
clearAndUpdatePageLayout(doc, layout);
int currentPage = 0;
Row currentRow = firstRow;
for (int i = 0; i <= 50; i++)
{
    Row clonedRow = (Row)firstRow.deepClone(true);
    Row insertedRow = (Row)table.insertAfter(clonedRow, currentRow);
    currentRow = insertedRow;
    if (updatedSummaryRow != null)
    {
        try
        {
            clearAndUpdatePageLayout(doc, layout);
            currentPage = layout.getEndPageIndex(insertedRow);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (currentPage != startPage)
        {
            startPage = currentPage;
            Row clonsedLastRow = (Row)updatedSummaryRow.deepClone(true);
            Node lastRow1 = table.insertBefore(clonsedLastRow, insertedRow.getPreviousSibling());
            getSummary(table, clonsedLastRow, floatingFirstRowSum, doc, layout, false, currentPage);
            Row clonsedSummaryRow = (Row)lastRow1.deepClone(true);
            table.insertAfter(clonsedSummaryRow, lastRow1);
        }
    }
}

try
{
    doc.save("C:\\Temp\\template111.docx");
    System.out.println("saved");

}
catch (Exception e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Hi @alexey.noskov, I am still getting this error
Exception occured while calculating summary of the page java.lang.RuntimeException: java.lang.NullPointerException
updating, SummaryColumn
Exception occured while calculating summary of the page java.lang.RuntimeException: java.lang.NullPointerException
updating, SummaryColumn
Exception occured while calculating summary of the page java.lang.RuntimeException: java.lang.NullPointerException
Exception in thread “main” java.lang.IllegalArgumentException: The reference node is not a child of this node.
at com.aspose.words.CompositeNode.zzZ(Unknown Source)
at com.aspose.words.CompositeNode.insertAfter(Unknown Source)
at watermarkAspose.Template.main(Template.java:147)

can you send me you generated document?

@sindhuV Here is the document generated on my side. template111.docx (10.6 KB)
Which version of Aspose.Words do you use? I used the latest 21.12 version for testing.

I am using 19.10. i will try with latest version once and let you know