-3

I am receiving this error Object reference not set to an instance of an object. When I am trying my code:

 for (int i = 1; i < dgv.RowCount; i++)
       {
        rtb.Text = rtb.Text+ dgv.Rows[i].Cells[0].Value.ToString();
               }

Where rtb is a richtextbox and dgv is a datagridview with 4 columns . Ty for the help

Xm7X
  • 861
  • 1
  • 11
  • 23
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jonesopolis Nov 10 '14 at 16:05
  • 1
    One (or more) of the following is `null`: `dgv.Rows[i].Cells[0]`, `dgv.Rows[i].Cells[0].Value`. Run the app in the debugger and see which is null. – D Stanley Nov 10 '14 at 16:09
  • It could really be a few things, fire up the debugger and if you cannot do that, then break down each item into a variable temporarily. – JL. Nov 10 '14 at 17:31

1 Answers1

1

Probably need to start your for loop from 0 and set the upper limit to RowCount -1 i.e.

for (int i = 0; i < dgv.RowCount - 1; i++)

C# uses zero based arrays.

chead23
  • 1,859
  • 11
  • 12