简体   繁体   English

string.IsNullOrWhiteSpace()和string.IsNullOrEmpty()中的NullReferenceException

[英]NullReferenceException in string.IsNullOrWhiteSpace() and string.IsNullOrEmpty()

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 . 我正在检查可能为空或为null / null的列的单元格的单元格值,因此我需要一些避免NullReferenceException

How do I do that since even with the IsNullOrWhiteSpace() and IsNullOrEmpty() I get that exception somehow. 我该怎么做,因为即使使用IsNullOrWhiteSpace()IsNullOrEmpty()IsNullOrEmpty()以某种方式得到该异常。

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 . 我已经尝试过尝试将这些方法放入==null ,我已经尝试过!=null What else is there or what am I doing wrong exactly and how do I do it right? 还有什么,或者我到底在做什么错,我该怎么做正确呢?

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: 我相信你不需要ToString()只需输入:

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

in your if statement do this: 在您的if语句中执行以下操作:

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

Many people don't understand how to diagnose a NullReferenceException . 许多人不了解如何诊断NullReferenceException Consider the following: 考虑以下:

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

Many parts of this could be null . 其中很多部分可能为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 . 如果anull ,则a[0]将抛出NullReferenceException If b is null , then b.Cells will throw a NullReferenceException , etc. 如果bnull ,则b.Cells将抛出NullReferenceException ,等等。

You simply have to figure out which of these is null in your particular situation. 您只需要弄清楚在您的特定情况下其中哪个为null 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 . 当找到null ,可以停止寻找NullReferenceException

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. 如果value为null,则将不评估ToString(),并且程序无法引发NullReferenceException。

I think in dataGridView1.Rows[0].Cells[8].Value.ToString() you will get a NullReferenceException if the Value is null. 我认为在dataGridView1.Rows[0].Cells[8].Value.ToString() ,如果Value为null,则会得到NullReferenceException。 So you should check for dataGridView1.Rows[0].Cells[8].Value != null and then you can convert it to a string 因此,您应该检查dataGridView1.Rows[0].Cells[8].Value != null ,然后可以将其转换为字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM