How to resolve Object Reference Not Set To An Instance Of An Object

Asked

Viewed 53,928 times

2


I have a serious problem. I have a Gridview populated with data from a Mysql BD working perfectly.
I also have a button to turn it into a table to be inserted into a PDF using iTextSharp that also works perfectly, EXCEPT when I perform a date-specific filter on this Grid, even if the correct data is shown to me when performing the filter. I also have another type of filter that works perfectly when I try to generate PDF. Error occurs only when I set a date range, whatever. The message "Object Reference Not Set To An Instance Of An Object" appears is displayed and as much as you have already searched, I have not found solution for my case.
I leave here the part of the code that points the error.
The line "table.Addcell(new Phrase(dataGridView1[n, j].Value.Tostring());" is indicated by Visual Studio as the problem.

for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
    for (int n = 0; n < dataGridView1.Columns.Count; n++)
    {
        tabela.AddCell(new Phrase(dataGridView1[n, j].Value.ToString()));
        tabela.HorizontalAlignment = 0;
        tabela.WidthPercentage = 100;
    }
}


Reinforcement: ONLY When I filter between dates, the error is shown! The Code in question is on the PDF generate button.
Note: I am using Windowsform

  • 1

    Alias NullReferenceException.

1 answer

6


This exception occurs when you call a method (or, more generally, reference) to an object null. In the case of the line where the error occurs, one of the objects (tabela, dataGridView1, dataGridView1[n, j] or dataGridView1[n, j].Value) is null. It’s best to find out which one it is.

It is almost certain that tabela it is not, otherwise it would never work, whether there is date filter or not.

dataGridView, also, would have caused trouble in the first for was null (since it refers to the collection dataGridView1.Rows in it.

Like n and j are restricted to the limits on dataGridView1.Rows.Count and dataGridView1.Columns.Count, respectively, it is not likely that we have exceeded the limits of the respective collections. So it remains that the problem should be in dataGridView1[n, r].Value, that is, for some value of n and r the result of Value must be null.

There are two ways to solve this problem. The first is by using the coalescence operator ??, thus:

tabela.AddCell(new Phrase((dataGridView1[n, j].Value ?? "").ToString()));

Alternatively, and in a more secure way, references to the method can (should) be replaced φ.ToString() by calls to the method Convert.ToString(φ), that doesn’t work when φ is void:

tabela.AddCell(new Phrase(Convert.ToString(dataGridView1[n, j].Value)));
  • Even if the limits of the condition were exceeded, there would be no Nullreferenceexception. From qqr form your final assumption is most likely where the problem is.

  • @jbueno You’re right, probably in this case would be IndexOutOfRangeException.

  • Thank you very much jbueno. Solved my problem. I appreciate the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.