0

I have the following code:

        foreach (DataGridViewRow r in DataGridObject.Rows)
        {
            MyDataSource.ForEach( 
                delegate( Product p)
                {
                    if (r.Cells["Product"].Value.ToString() == p.Title)
                    {
                        tally += p.Price;
                    }
                }
            );
        }

At the if statement on run time, I am getting the error:

An unhandled exception of type 'System.NullReferenceException' 
occurred in WindowsFormsApplication1.exe

Additional information: Object reference not set to an instance of an object.

What might be the problem ?

Any thought where i went wrong?

Arun Chandran C
  • 3,306
  • 7
  • 28
  • 44
Jimmyt1988
  • 18,656
  • 34
  • 113
  • 210
  • 1
    That `r.Cells["Product"]` is null and you are trying to access `Value` or `r.Cells["Product"].Value` is null and you are trying to call `ToString()` on it – Charleh Jul 16 '13 at 16:58
  • Could also be that `p` is null, but less likely - have you tried debugging? – Charleh Jul 16 '13 at 16:58
  • Well. r.Cells[ "Product" ] is not doing quite what I thought it should then. How do I get the value from a column? – Jimmyt1988 Jul 16 '13 at 17:00
  • r.Cells["Product"].Value could be null – Sriram Sakthivel Jul 16 '13 at 17:06
  • Do you have a column with "Product" exactly? Can you try index instead? 'r.Cells[1].Value' something like that? – seshuk Jul 16 '13 at 17:13
  • 1
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jul 16 '13 at 17:19

4 Answers4

1

do it like this

             foreach (DataGridViewRow r in DataGridObject.Rows)
    {
        MyDataSource.ForEach( 
            delegate( Product p)
            {
                if (!string.isNullorWhiteSpace(Convert.ToString(r.Cells["Product"].Value)) && Convert.ToString(r.Cells["Product"].Value).Equals(p.Title,StringComparison.OrdinalIgnoreCase))
                {
                    tally += p.Price;
                }
            }
        );
    }
Ehsan
  • 28,801
  • 6
  • 51
  • 61
0

Do you have a column with "Product" exactly? Can you try index instead?

r.Cells[1].Value

something like that?

Also, you might have to convert it into a particular cell type? Ex: if its a textboxcell then you might have to do

r.Cells[1].Text
seshuk
  • 162
  • 4
0
 foreach (DataGridViewRow r in DataGridObject.Rows)
        {
            MyDataSource.ForEach( 
                delegate( Product p)
                {
                    if(r.Cells["Product"]!=null)
                     {
                      if(r.Cells["Product"].Value!=null)
                      {
                        if (r.Cells["Product"].Value.ToString() == p.Title)
                        {
                          tally += p.Price;
                        }
                      }
                    }
                }
            );
        }
huMpty duMpty
  • 13,481
  • 12
  • 52
  • 88
0

need to check cell.Value for null

delly47
  • 27
  • 4
  • I was trying to edit the guys answer but he wont accept it, so im gnna make yours the answer. – Jimmyt1988 Jul 17 '13 at 15:07
  • @JamesT: Basically the `Moderators` has reviewed your edit and rejected. Not me !! But I suggest you to have `null` check on both `r.Cells["Product"]` and `r.Cells["Product"].Value` – huMpty duMpty Jul 17 '13 at 15:50
  • Well why on earth are they doing that. I'm so sorry brah! I personally think your answer is far more comprehensive. Perhaps i'll now suffer the penalty but im going to mark yours as the correct answer. – Jimmyt1988 Jul 17 '13 at 15:56