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