-1

Using aspose-words-16.2.0-jdk16.jar

Getting below exception on aspose word document save

Word Document Link : https://1drv.ms/w/s!AjRHZK-1fb7PjzH14y9bTaFO9jjK

Created below class SampleApplicationTest to save the document :

Java Code:

 public class AsposeTableTest {

    public static void main(String[] args) {
        try {
            Document doc=new Document("D:/MultipleRequestResponse.docx");
            createTableToReplace(doc,2,0);
            doc.save("D:/SavedDoC.docx");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void createTableToReplace(Document sourceDocument, int noofCount)
            throws Exception {
        try{
            Document doc = sourceDocument;
            DocumentBuilder builder = new DocumentBuilder(doc);

            Section section = doc.getFirstSection();
            // Quick typed access to the Body child node of the Section.
            Body body = section.getBody();

            // Quick typed access to all Table child nodes contained in the
            // Body.
            TableCollection tables = body.getTables();
            System.out.println(tables.getCount());

            for (Table table : tables.toArray()) {

                // Quick typed access to the first row of the table.
                if (table.getFirstRow() != null) {
                    if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
                            .equalsIgnoreCase("~Multiple~")) {
                        if (table.getNextSibling() != null) {
                            builder.moveTo(table.getNextSibling());
                        } else {
                            builder.moveToDocumentEnd();
                        }
                        for (int i = 0; i < noofCount; i++) {

                            Table table1 = builder.startTable();
                            for (int j = 0; j < table.getRows().getCount(); j++) {
                                Row row = (Row) table.getRows().get(j)
                                        .deepClone(false);
                                table1.appendChild(row);
                                if (!"~Multiple~".equalsIgnoreCase(table.getRows()
                                        .get(j).toString(SaveFormat.TEXT).trim())) {
                                    for (int k = 0; k < table.getRows().get(j)
                                            .getCells().getCount(); k++) {
                                        Cell newcell = (Cell) table.getRows()
                                                .get(j).getCells().get(k)
                                                .deepClone(false);
                                        row.getCells().add(newcell);
                                        Paragraph para = new Paragraph(doc);
                                        Run run = new Run(doc, table.getRows()
                                                .get(j).getCells().get(k)
                                                .toString(SaveFormat.TEXT).trim());

                                        para.appendChild(run);
                                        newcell.appendChild(para);

                                    }
                                }
                            }

                        }

                    }

                }

                if (table.getFirstRow() != null) {
                    if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
                            .equalsIgnoreCase("~Multiple~")) {
                        table.removeAllChildren();
                    }
                }

            }
        }
        catch(Exception e){
            throw e;
        }
    }

}

Exception Stack Trace:

java.lang.NullPointerException
at com.aspose.words.zzX.zzZ(Unknown Source)
at com.aspose.words.zzX.zzZ(Unknown Source)
at com.aspose.words.zzX.zzX(Unknown Source)
at com.aspose.words.zzZ1M.zzw(Unknown Source)
at com.aspose.words.zzZ1M.visitTableStart(Unknown Source)
at com.aspose.words.zzBB.visitTableStart(Unknown Source)
at com.aspose.words.Table.zzZ(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Table.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Body.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Section.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Document.accept(Unknown Source)
at com.aspose.words.zzBB.zzY(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at AsposeTableTest.main(AsposeTableTest.java:22)

Note: Path is not null and doc is not null.

What is the cause of above exception ?

Please assist me to resolve this issue.

Mihir
  • 554
  • 6
  • 23
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – piet.t Mar 12 '18 at 14:17
  • 1
    the problem is here: `at com.aspose.words.ëQF.ëZ(Unknown Source)` – Timothy Truckle Mar 12 '18 at 14:19
  • 4
    This is a concrete error, but one where we probably cannot do much without additional information. Aspose has support forums for both free and paid customers, try seeking their targeted help first: https://forum.aspose.com/c/words (free), https://helpdesk.aspose.com/ (paid). You can always try a decompiler, too, but the class and method names seem to be obfuscated, so good luck there :(. – Petr Janeček Mar 12 '18 at 14:23
  • @PetrJaneček beat me to it, check support or the official forums if you suspect this is an issue with Aspose. – an earwig Mar 12 '18 at 14:24
  • Well, while this could ba a concrete error for the OP all we know is that some NPE happened somewhere and that something called `Path` and something called `doc` but that we don't know anything about is supposed to be not null... plus the stacktrace seems to be incomplete.... – piet.t Mar 12 '18 at 14:30
  • 1
    Please share your input Word document here for testing. I will investigate the issue on our side and provide you more information. I work with Aspose as Developer Evangelist. – Tahir Manzoor Mar 12 '18 at 16:33
  • @TahirManzoor I shared link for the sample word document.Created a sample application posted the code and the exception stack trace.Please assist me to resolve the issue. – Mihir Mar 13 '18 at 05:54

1 Answers1

1

Please use the following modified method to fix this issue.

public static void createTableToReplace(Document sourceDocument, int noofCount)
        throws Exception {
    try{
        Document doc = sourceDocument;
        DocumentBuilder builder = new DocumentBuilder(doc);

        Section section = doc.getFirstSection();
        // Quick typed access to the Body child node of the Section.
        Body body = section.getBody();

        // Quick typed access to all Table child nodes contained in the
        // Body.
        TableCollection tables = body.getTables();
        System.out.println(tables.getCount());

        for (Table table : tables.toArray()) {

            // Quick typed access to the first row of the table.
            if (table.getFirstRow() != null) {
                if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
                        .equalsIgnoreCase("~Multiple~")) {

                    for (int i = 0; i < noofCount; i++) {

                        Table cloneTable = (Table)table.deepClone(true);
                        //Remove the first row of table if you want.
                        cloneTable.getFirstRow().remove();
                        table.getParentNode().insertAfter(cloneTable, table);
                        //table.getParentNode().insertAfter(new Paragraph((doc)), table);
                    }
                }
            }

            if (table.getFirstRow() != null) {
                if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
                        .equalsIgnoreCase("~Multiple~")) {
                    for(Row row : table.getRows())
                    {
                        if(row.isFirstRow())
                            continue;
                        else
                            row.remove();
                    }
                    //table.remove();
                }
            }
        }
    }
    catch(Exception e){
        throw e;
    }
}

I work with Aspose as Developer Evangelist.

Tahir Manzoor
  • 577
  • 2
  • 9
  • Expected output Document should be https://1drv.ms/w/s!AjRHZK-1fb7PjzII1kG1JQu7w7al instead of that it is repeating the tables. – Mihir Mar 13 '18 at 12:36