Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView.Invoke引发NullReferenceException_C#_.net_Multithreading - Fatal编程技术网

C# DataGridView.Invoke引发NullReferenceException

C# DataGridView.Invoke引发NullReferenceException,c#,.net,multithreading,C#,.net,Multithreading,我对DataGridView.Invoke有问题 delegate void CheckMainTableCallback(); static void CheckMainTable() { if (Program.MonitorApp.ServersTable.InvokeRequired) { CheckMainTableCallback Safe = new CheckMainTableCallback(CheckMainTable); Mo

我对DataGridView.Invoke有问题

delegate void CheckMainTableCallback();
static void CheckMainTable()
{
    if (Program.MonitorApp.ServersTable.InvokeRequired)
    {
        CheckMainTableCallback Safe = new CheckMainTableCallback(CheckMainTable);
        MonitorApp.ServersTable.Invoke(Safe);
    }
    else
    {
        foreach (DataGridViewRow r in MonitorApp.MainTable.Rows)
        {
            **r.Cells["Load"].Value = 
                (Servers.Find(
                    p => p.NAME == r.Cells[0].ToString()
                )
                .GetSystemValue("% Proccess Usage"));** // exception here
        }
    }
}
因此,我调用CheckMainTable以从ServersTable(由另一个线程使用)获取值到我的MainTable。 它抛出了一个空引用。
我做错了什么?

所以跟踪这样的异常是一个逐件的过程。请考虑这一行代码:

r.Cells[0].ToString()
如果
r.Cells[0]
null
则可能引发


此外,
.GetSystemValue
将抛出来自服务器的结果。Find(…were
null

您的代码的哪部分抛出异常?@DeeMac r.Cells[“Load”].Value=(Servers.Find(p=>p.NAME==r.Cells[0].ToString()).GetSystemValue(%Proccess Usage)));代码的哪一部分?尽可能具体。调试时,哪个属性引用会出现此异常?@DeeMac here:p.NAME==r.Cells[0]。ToString()。我认为这是一个线程冲突,因为读取p.NAME字段。谢谢,你给了我解决问题的正确方向,下面是答案:p.NAME==r.Cells[0]。Value.ToString()@Greag.Deay,是的,您也可以使用
Convert.ToString(r.Cells[0].Value)。这样,如果Cells值为
null`它就不会抛出。