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.

Thu Nov 07, 2019 5:20 am

I am creating a table and trying to add it to a spire document.

However, when I try to set the layout to autofit to contents, I get an "Object reference is null error" that comes from within spire.doc assemblies.
No further information is given.

The code I'm using is below:


private Table createTable(string tableJson, Document document)
{

JArray jsonTable = (JArray)JsonConvert.DeserializeObject(tableJson);
JArray rowtable = (JArray)jsonTable[0];
int rowCount = jsonTable.Count();
int cellCount = rowtable.Count();

Table table = new Table(document, true, 1f);
table.ResetCells(rowCount, cellCount);

populateCells(table, jsonTable);

table.AutoFit(AutoFitBehaviorType.AutoFitToContents);
return table;

}

kaimon
 
Posts: 16
Joined: Fri May 17, 2019 4:04 am

Thu Nov 07, 2019 8:30 am

Hi,

Thanks for your inquiry.
After testing your case with the latest Spire.Doc Pack(hot fix) Version:7.11.1, I didn't reproduce your issue. Below is my full code:
Code: Select all
            Document doc = new Document();
            Section section = doc.AddSection();
            Table table = section.AddTable(true);
            table.ResetCells(3, 5);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                TableRow row = table.Rows[i];
                for (int j = 0; j < row.Cells.Count; j++)
                {
                    TableCell cell = row.Cells[j];
                    cell.AddParagraph().AppendText("Row" + i.ToString() + "cell" + j.ToString());
                }
            }

            table.AutoFit(AutoFitBehaviorType.AutoFitToContents);
            doc.SaveToFile("19500.docx",FileFormat.Docx);


I firstly suggest you try to use the latest version. If the issue still exists, please provide full code or a sample project for showing your issue.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Nov 13, 2019 9:23 am

Hi,

Greetings from E-iceblue.
Did the code I provided help you solve your issue?
Could you please give us some feedback at your convenience?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Nov 19, 2019 2:06 am

HI,

Sorry the 7.11.1 hotfix didn't work.

I'm getting the same "object reference not set error".

When I look at the exception stack, its being raised from within your Table class

at Spire.Doc.Table.ᜉ()
at Spire.Doc.Table.ᜊ()
at Spire.Doc.Table.ᜀ(AutoFitBehaviorType A_0)
at Spire.Doc.Table.ᜏ()
at Spire.Doc.Table.AutoFit(AutoFitBehaviorType behavior)

kaimon
 
Posts: 16
Joined: Fri May 17, 2019 4:04 am

Tue Nov 19, 2019 2:10 am

Is there a mode I can set to get the returned exception to be more detailed?

kaimon
 
Posts: 16
Joined: Fri May 17, 2019 4:04 am

Tue Nov 19, 2019 2:29 am

Hi,

Do you try the code I provided? Does it work?
As for your issue, could you please provide complete code or a sample project for testing?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Nov 19, 2019 2:42 am

I've further debugged this and it seems the autofit call will fail if the table hasn't been created using a Section.AddTable method.

In my original code, I created the table directioned by instantiating a new Table().

However, even though i no longer get the object reference not set error, it looks like the table i'm trying to AutoFit to contents doesn't display correctly even after making the call.

Happy to send you my sample doc files if you can give me an email address..

kaimon
 
Posts: 16
Joined: Fri May 17, 2019 4:04 am

Tue Nov 19, 2019 3:05 am

Hi,

Please send your sample project to support@e-iceblue.com via email.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Nov 19, 2019 3:19 am

I've managed to get this working.

It seems like it was an issue with my word docs styling.

kaimon
 
Posts: 16
Joined: Fri May 17, 2019 4:04 am

Tue Nov 19, 2019 3:30 am

Hi,

Glad to hear that you have managed to solve your issue.
If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc