0

I'm checking the cell values of cells of a column that might or not be empty/null so I needed something to avoid a NullReferenceException.

How do I do that since even with the IsNullOrWhiteSpace() and IsNullOrEmpty() I get that exception somehow.

Here's part of the code I'm using:

s = "Total = " + dataGridView1.Rows[0].Cells.Count +
    "0 = " + dataGridView1.Rows[0].Cells[0].Value.ToString() +
    "/n  1 = " + dataGridView1.Rows[0].Cells[1].Value.ToString() +
    "/n  2= " + dataGridView1.Rows[0].Cells[2].Value.ToString() +
    "/n  3 = " + dataGridView1.Rows[0].Cells[3].Value.ToString() +
    "/n  4= " + dataGridView1.Rows[0].Cells[4].Value.ToString() +
    "/n  5 = " + dataGridView1.Rows[0].Cells[5].Value.ToString() +
    "/n  6= " + dataGridView1.Rows[0].Cells[6].Value.ToString() +
    "/n  7 = " + dataGridView1.Rows[0].Cells[7].Value.ToString();

    if (string.IsNullOrEmpty(dataGridView1.Rows[0].Cells[8].Value.ToString()))
    {

    }
    else
    {
        s += "/n  8 = " + dataGridView1.Rows[0].Cells[8].Value.ToString();
    }

I've tried those methods I've tried putting it ==null, I've tried !=null. What else is there or what am I doing wrong exactly and how do I do it right?

Grant Winney
  • 61,140
  • 9
  • 100
  • 152
  • 2
    I usually use `as` in this situation: `string.IsNullOrEmpty(dataGridView1.Rows[0].Cells[8].Value as string)`. `as` will return `null` if `Value` is not a string or is null. Also assuming of course that this particular exception is because `Value` is null. Do note, however, that my example is semantically different from attempting to call `ToString` on something, as that will work for any type. – Adam Houldsworth Oct 31 '13 at 14:51
  • 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 Oct 31 '13 at 14:52
  • `.ToString()` _never_ returns `null` (in the .NET Framework - you could of course write one that does), so your test with `.IsNullOrEmpty` is pointless against null. In fact, you probably get the exception from calling `.ToString()` on a value that is `null`. – Kris Vandermotten Oct 31 '13 at 14:54

4 Answers4

4

the are a lot of places you code could throw that exception ..

dataGridView1.Rows[0]  //here
             .Cells[0] //here
             .Value    //and here
             .ToString() 

I belive you don't need the ToString() just type:

"... "+ dataGridView1.Rows[0].Cells[0].Value

in your ifstatement do this:

if (string.IsNullOrEmpty(dataGridView1.Rows[0].Cells[8].Value as string))
Jens Kloster
  • 10,267
  • 4
  • 36
  • 51
2

Many people don't understand how to diagnose a NullReferenceException. Consider the following:

dataGridView1.Rows[0].Cells[3].Value.ToString()

Many parts of this could be null. It's the same thing as

var a = dataGridView1.Rows;
var b = a[0];
var c = b.Cells;
var d = c[3];
var e = d.Value;
var f = e.ToString();

If a is null, then a[0] will throw a NullReferenceException. If b is null, then b.Cells will throw a NullReferenceException, etc.

You simply have to figure out which of these is null in your particular situation. The simplest way is to use the debugger. Set a breakpoint before the line that throws the exception. Then hover the mouse over various parts of the expression to see which are null, or use the "Watch" window to enter parts of the expression.

When you find a null, you can stop looking for your NullReferenceException.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
0

You can add an extra line of code to check and handle the null case.

var value = dataGridView1.Rows[0].Cells[0].Value;
string s = (value == null ? string.Empty : value.ToString());

If value is null, then ToString() will not be evaluated and the program cannot throw NullReferenceException.

Alex Walker
  • 2,132
  • 1
  • 14
  • 27
0

I think in dataGridView1.Rows[0].Cells[8].Value.ToString() you will get a NullReferenceException if the Value is null. So you should check for dataGridView1.Rows[0].Cells[8].Value != null and then you can convert it to a string

Tomtom
  • 8,386
  • 7
  • 45
  • 83