java.lang.NullPointerException at com.aspose.words.ëQF.ëZ



Using aspose-words-16.2.0-jdk16.jar

保存 Word 文档时出现以下异常

Word文档链接:https://1drv.ms/w/s!AjRHZK-1fb7PjzH14y9bTaFO9jjK

在类SampleApplicationTest下面创建以保存文档:

爪哇代码:

 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;
        }
    }
}

异常堆栈跟踪:

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)

注意:路径不为空,文档不为空。

上述异常的原因是什么?

请协助我解决此问题。

请使用以下修改后的方法解决此问题。

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;
    }
}

我作为开发人员布道者与Aspose合作。

最新更新