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

AddRow throws null reference exception
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3912
Page 1 of 1

Author:  blake.causey [ Tue Feb 05, 2019 6:17 pm ]
Post subject:  AddRow throws null reference exception

In the "Table" class, the "AddRow" method will occasionally throw a null reference exception because "this.rows" is null.

The following is the "AddRow" method and notice it references the internal property "rows" but it is null at times.
Code:
 /// <summary>
 /// Adds a new row to the table. Allowed only if at least one column was added.
 /// </summary>
 public Row AddRow()
 {
   return this.rows.AddRow();
 }

The "getter" for "Rows" property in the "Table" class handles this by first checking for null and if not then it initializes it. The problem is that the method "AddRow" doesn't use the getter or checks for null first. Therefore, it throws an exception.
Code:
/// <summary>Gets the Rows collection of the table.</summary>
public Rows Rows
{
    get
    {
        if (this.rows == null)
            this.rows = new Rows((DocumentObject) this);
        return this.rows;
    }
    set
    {
        this.SetParent((DocumentObject) value);
        this.rows = value;
     }
}

Shouldn't the method "AddRow" check for null first like the "getter"?

As a workaround all you have to do is access the "Rows" "getter" before making a call to "AddRow" and the problem is avoided because the "getter" initializes the value "rows". However, it would be nice to not have to think about doing that.

Author:  TH-Soft [ Tue Feb 05, 2019 7:47 pm ]
Post subject:  Re: AddRow throws null reference exception

Hi!
blake.causey wrote:
However, it would be nice to not have to think about doing that.
I've been using MigraDoc for years and I never had this exception.

Thanks for your feedback. We'll include that fix in future versions of MigraDoc.

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