Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Jul 01, 2024 4:34 pm

Hi,
I've been looking for a way to create a Table of content for my Word document. I have placed this code block to add the table of contents:

Code: Select all
Section section = doc.addSection();
            TableOfContent table = new TableOfContent(doc, "{\\o \"1-9\" \\h \\z \\u}");           
            Paragraph paragraph = section.addParagraph();
            paragraph.appendTOC(1, 2);
            paragraph.getItems().add(table);
            doc.updateTableOfContents();


It gives me an error:
java.lang.NullPointerException: Cannot invoke "com.spire.doc.packages.sprhds.spr??()" because the return value of "com.spire.doc.packages.sprwks.spr??()" is null

I have previously created several sections in my document.

Sysviewsoft
 
Posts: 1
Joined: Mon Jun 24, 2024 6:57 pm

Tue Jul 02, 2024 2:18 am

Hi,

Thank you for your inquiry.
Please append FieldMark before apdateTableOfContents(), you can refer code below for details.
If there's still any issue, please provide your test document for our further investigation and test. Thanks in advance.

Code: Select all
        Document doc = new Document();
        Section section = doc.addSection();
        TableOfContent table = new TableOfContent(doc, "{\\o \"1-9\" \\h \\z \\u}");
        Paragraph paragraph = section.addParagraph();
        paragraph.appendTOC(1, 2);
        paragraph.getItems().add(table);
        paragraph.appendFieldMark(FieldMarkType.Field_Separator);
        paragraph.appendFieldMark(FieldMarkType.Field_End);
        doc.updateTableOfContents();


Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.Doc