PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

NullReferenceException when calling RenderDocument
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1077
Page 1 of 1

Author:  complym [ Mon Mar 01, 2010 5:10 pm ]
Post subject:  NullReferenceException when calling RenderDocument

Hi there,

I'm am completely at a loss with a problem I am having with MigraDoc at the moment. I will try my best to explain, but please ask if you need anymore information. Sometimes when I call PdfDocumentRenderer's RenderDocument function, I am getting a NullReferenceException (Object reference not set to an instance of an object). I have managed to narrow this down to being something to do with when I'm adding a table to a section.

It makes no sense to me what so ever. The problem is intermittent and the same code is used everytime it is called. It can be called 8 times in the same document and work 7 of those times! By process of elimination, this line of code is what seems to be the cause of the exception:

Code:
table = section.AddTable();
(where table is of type Table and section of type Section).

And the code that actually throws the exception is (on the pdfRender.RenderDocument() line):

Code:
MigraDoc.Rendering.PdfDocumentRenderer pdfRender = new MigraDoc.Rendering.PdfDocumentRenderer(false, PdfSharp.Pdf.PdfFontEmbedding.Always);
            pdfRender.Document = document;
            pdfRender.RenderDocument();


Any idea's or suggestions you may have would be massively appreciated as I am now fresh out of ideas!

Thanks in advance,
Sarah

Author:  Thomas Hoevel [ Tue Mar 02, 2010 8:54 am ]
Post subject:  Re: NullReferenceException when calling RenderDocument

Hi, Sarah,

our application uses a lot of tables - and rendering works 10 out of 10 times.

In Visual Studio, click Exceptions in the Debug menu and check Common Language Runtime Exceptions in the Thrown column.

Then you'll see where the exception is thrown. This may help us further.

The exception relates to the contents of your table.
There's nothing wrong with the source lines you show us. The problems come from the lines you don't show ...

Author:  complym [ Tue Mar 02, 2010 9:11 am ]
Post subject:  Re: NullReferenceException when calling RenderDocument

Thanks for your reply Thomas. I've checked the Throw box in the debug menu but it's stopped in the same place on RenderDocument. I'm guessing I need to load the MigraDoc project so I can go into that code as at the moment I am just using the DLL's so I can't step into them or anything. I will give that a go now and let you know how I get on.

I thought it may have been the data I was putting in the table, so I tried drawing the table without giving it any data and it still gave me the error. My application creates a report based on date ranges, and here's the really strange thing! If I try and create it for 22nd March 09 - 01st April 09, it will fail to draw this one table which has 3 rows of information. It will draw other tables using the same code no problem. If I try and create it for 23rd March 09 - 01st April 09 it will work and draw the table, and the table has exactly the same data plus an extra row of new data for the 23rd. However I know it's not down to the number of rows, as the same code will draw a table with 1, 2 or 3 rows in other cases without any problems.

Anyway, I'll let you know how I get on with debugging! Thanks again :)

Author:  complym [ Tue Mar 02, 2010 9:25 am ]
Post subject:  Re: NullReferenceException when calling RenderDocument

Ok so the exception is being thrown on this line:

probeHeight = (XUnit)this.bottomBorderMap[probeRow + 1] - offset;

Which is in TableRenderer. None of the variables are null though, they all show a value. I really appreciate your help! Please let me know if you need any more information.

Thanks,
Sarah.

Author:  complym [ Tue Mar 02, 2010 9:28 am ]
Post subject:  Re: NullReferenceException when calling RenderDocument

Here's the stack trace in case that helps...

at MigraDoc.Rendering.TableRenderer.Format(Area area, FormatInfo previousFormatInfo) in \MigraDoc\PDFSharp-MigraDocFoundation-1_31\MigraDoc\code\MigraDoc.Rendering\MigraDoc.Rendering\TableRenderer.cs:line 328

Author:  Thomas Hoevel [ Tue Mar 02, 2010 9:55 am ]
Post subject:  Re: NullReferenceException when calling RenderDocument

Plain vanilla tables work - so there must be something unusual in your table code.

Do you use MergeDown?
There's a known problem with certain combinations of MergeDowns.

An MDDDL file of your document could help.
http://www.pdfsharp.net/wiki/MigraDocDDL.ashx

You can attach a ZIP to this thread or send it via PM.

Author:  complym [ Tue Mar 02, 2010 10:58 am ]
Post subject:  Re: NullReferenceException when calling RenderDocument

Thanks Thomas. I have attached the DDL. I'm not using a merge down in this table but I am using a merge right. The DDL attached is only creating the table that is causing the problems. If you like me to create another one where it also creates a table successfully, please let me know.

Thanks again,
Sarah

Attachments:
ReportMDDDL.zip [1.18 KiB]
Downloaded 480 times

Author:  complym [ Wed Mar 03, 2010 2:38 pm ]
Post subject:  Re: NullReferenceException when calling RenderDocument

I think I may have solved it! I'm using the keep with property so that if it can't draw a certain amount of rows on a page, it will start the table on a new page. My original code was:
Code:
if (table.Rows.Count < 5)
                table.Rows[0].KeepWith = table.Rows.Count - 1;
            else
                table.Rows[0].KeepWith = 5;

I noticed in the MDDDL file that I sent you, compared with one that was successful that this was different and commented out the keep with code, and the table that was originally failing worked. So I tried changing the code to the following:
Code:
if (table.Rows.Count <= 5)
                table.Rows[0].KeepWith = table.Rows.Count - 1;
            else
                table.Rows[0].KeepWith = 5;

And it worked! It'll require a lot more testing but fingers crossed. I'll let you know how I get on. If you have any suggestions on how to improve my use of the keep with property to make my code more robust, all suggestions are gratefully received :)

Thanks,
Sarah

Author:  Thomas Hoevel [ Wed Mar 03, 2010 2:47 pm ]
Post subject:  Re: NullReferenceException when calling RenderDocument

Code:
table.Rows[0].KeepWith = 5;

requires 6 rows in the table.
So the old code will always fail if there are 5 rows in the table.

Thank you for the feedback.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/