PDFsharp & MigraDoc Foundation

AddRow throws null reference exception
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.
 /// <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.
/// <summary>Gets the Rows collection of the table.</summary>
public Rows Rows
        if (this.rows == null)
            this.rows = new Rows((DocumentObject) this);
        return this.rows;
        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

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