Click here to Skip to main content
15,917,321 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am having a problem when trying to read cells in a DataGridView.

When the program executes the code below...

C#
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (dataGridView1.Rows[i].Cells[0].Value.ToString().Length == 0 && dataGridView1.Rows[i].Cells[1].ToString().Length != 0)
    {
        MessageBox.Show("You must enter a description for each value.");
        return;
    }
    if (dataGridView1.Rows[i].Cells[1].Value.ToString().Length == 0 && dataGridView1.Rows[i].Cells[0].ToString().Length != 0)
    {
        MessageBox.Show("You must enter a value for each description.");
        return;
    }
}


The following error appears:

Object reference not set to an instance of an object.


What I do is execute the program and type 'test' (for example), in the first field [Description]. When i Click on the button which activates the verification, the error happens.

Can someone help me? :confused:

I am using Visual Studio Team System - C#

Thanks in advance! :)
Posted
Updated 1-Nov-10 2:27am
v5

Object reference not set to an instance of an object


It's a generic error which occurs when you are having any uninitialized or null valued object for which you are going to apply any operations. That's why It is considered as Exception of kind NullReferenceException.

To overcome this error always check 'Null' on the object before any operation done on it.

You could have more clarity with NullReferenceException with THIS[^] article.


Please vote and Accept Answer if it Helped.
 
Share this answer
 
Don't Refer Cells With Index, Better to Specify Column Name's.

A small change to Mr.Abhishek answer
dataGrid1.Rows[i].Cells["ColumnName"].Value != null

Replace your code with:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
      if (!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Column1"].Value.ToString())&& !string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Column2"].Value.ToString())) //Above Statement is Used to Check For String is Null or Empty. By making use of string.IsnullorEmpty() We can Handle this kind of "Object Reference not set to instance of an object" situations. I am not sure how far My Code would be useful.. But have a look at this. 
       {
           if (dataGridView1.Rows[i].Cells["Column1"].Value.ToString().Length == 0
|| dataGridView1.Rows[i].Cells["Column1"].Value == null
&& dataGridView1.Rows[i].Cells[1].Value.ToString().Length != 0)
           {
                 MessageBox.Show("You must enter a description for each value.");
                 return;
           }
           if (dataGridView1.Rows[i].Cells["Column2"].Value.ToString().Length == 0 ||dataGridView1.Rows[i].Cells["Column2"].Value == null &&dataGridView1.Rows[i].Cells[0].Value.ToString().Length != 0)
           {
                 MessageBox.Show("You must enter a value for each description.");
                 return;
           }
     }
}
 
Share this answer
 
v6
Comments
Dalek Dave 1-Nov-10 5:43am    
Good Answer.
lucasgrohl 1-Nov-10 7:15am    
Pawan Kiran
I replaced the code for the one you pasted above. What is happening now is:

1 - If one one the columns is empty (not even has been clicked), occurs the same error
2 - If both columns are filled, it works for the lines that are filled. But, when the for command reaches the last line (the blank line dataGridView creates automatically), occurs the same error

I think the second case will be solved if first one is solved. But, I am not understanding why it is not working if I also included the 'null' verification in the if conditions.

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells["Column1"].Value.ToString().Length == 0 || dataGridView1.Rows[i].Cells["Column1"].Value == null && dataGridView1.Rows[i].Cells[1].Value.ToString().Length != 0)
{
MessageBox.Show("You must enter a description for each value.");
return;
}

if (dataGridView1.Rows[i].Cells["Column2"].Value.ToString().Length == 0 || dataGridView1.Rows[i].Cells["Column2"].Value == null && dataGridView1.Rows[i].Cells[0].Value.ToString().Length != 0)
{
MessageBox.Show("You must enter a value for each description.");
return;
}
}

Can you help me?

Thank you so much for the support you're giving me!
lucasgrohl 1-Nov-10 10:19am    
Thanks but the error occurs as well. I don't understand, do I have to initiate the Cells Content or any other thing?
Hi,
try this to find your error:

C#
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (dataGridView1.Rows[i].Cells[0] == null)
    {
        MessageBox.Show("Cells[0] is null");
        continue;
    }
    if (dataGridView1.Rows[i].Cells[1] == null)
    {
        MessageBox.Show("Cells[1] is null");
        continue;
    }
    if (dataGridView1.Rows[i].Cells[0].Value.ToString().Length == 0 && dataGridView1.Rows[i].Cells[1].ToString().Length != 0)
    {
        MessageBox.Show("You must enter a description for each value.");
        return;
    }
    if (dataGridView1.Rows[i].Cells[1].Value.ToString().Length == 0 && dataGridView1.Rows[i].Cells[0].ToString().Length != 0)
    {
        MessageBox.Show("You must enter a value for each description.");
        return;
    }
}
 
Share this answer
 
Comments
lucasgrohl 1-Nov-10 5:13am    
After I tried it, nothing happened. I mean, the code stopped at the same point as before.
I understand, in this case, both Cells are already initialized, right?
NullReferenceException can occur in many cases.
Always it is good to check an object before calling .ToString().Length like this.
So Please ensure dataGrid1.Rows[i].Cells[1].Value != null before you call ToString().
 
Share this answer
 
v2
Comments
lucasgrohl 1-Nov-10 5:01am    
I replaced the previous code for:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString().Length == 0 || dataGridView1.Rows[i].Cells[0].Value == null && dataGridView1.Rows[i].Cells[1].ToString().Length != 0)
{
MessageBox.Show("You must enter a description for each value.");
return;
}

if (dataGridView1.Rows[i].Cells[1].Value.ToString().Length == 0 || dataGridView1.Rows[i].Cells[0].Value == null && dataGridView1.Rows[i].Cells[0].ToString().Length != 0)
{
MessageBox.Show("You must enter a value for each description.");
return;
}
}

But the error persists =(
fjdiewornncalwe 1-Nov-10 10:29am    
if( string.IsNullOrEmpty( dataGridView1.Rows[i].Cells[0].Value ) ... You don't even have to check the length then, just do the IsNullOrEmpty check. It does both.
fjdiewornncalwe 1-Nov-10 10:30am    
As a second notation. Unless you can guarantee the existence of all the objects in the hierarchy of the call, you may want to do a null check on each level of the call as well just be sure where your reference error is occuring.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900