NullReferenceException未处理C#(使用BindingList)

时间:2015-05-31 03:31:46

标签: c# null nullreferenceexception

我正在尝试重新开发一个程序,但我无法弄清楚导致错误的原因:

  

对象引用未设置为对象的实例。

private void setDataSource(BindingList<ListObject> list)
{
    if(this.dataGridView1.InvokeRequired)
    {
        SetDSCallback method = new SetDSCallback(this.setDataSource);
        base.Invoke(method, new object[] { list }); <-- Error Here
    }
    else
    {
        this.dataGridView1.DataSource = null;
        this.dataGridView1.DataSource = list;
        this.dataGridView1.Columns["ip"].HeaderText = "External IP";
        this.dataGridView1.Columns["macAddress"].HeaderText = "MAC Address";
        this.dataGridView1.Columns["ipSource"].HeaderText = "IP Source";
        this.dataGridView1.Columns["ipDest"].HeaderText = "IP Destination";
        this.dataGridView1.Columns["portSource"].HeaderText = "Source Port";
        this.dataGridView1.Columns["portDest"].HeaderText = "Destination Port";
        this.dataGridView1.Columns["protocol"].HeaderText = "Protocol";
        this.dataGridView1.Columns["label"].HeaderText = "Label";
        this.dataGridView1.Columns["country"].HeaderText = "Country";
        this.dataGridView1.Columns["state"].HeaderText = "State";
        this.dataGridView1.Columns["city"].HeaderText = "City";
        this.dataGridView1.Columns["packetCount"].HeaderText = "Packets";
    }
}

如果他们愿意提供帮助,我愿意将整个项目发送给某人。 ListObject类是一类getter和setter。

1 个答案:

答案 0 :(得分:1)

将if,else块放入另一个if并检查if(list!= null)并将if else块放入其中。

    private void setDataSource(BindingList<ListObject> list)
        {
          if(list!=null)
           {
            if(this.dataGridView1.InvokeRequired)
            {
                SetDSCallback method = new SetDSCallback(this.setDataSource);
                base.Invoke(method, new object[] { list }); <-- Error Here
            }
            else
            {
                this.dataGridView1.DataSource = null;
                this.dataGridView1.DataSource = list;
                this.dataGridView1.Columns["ip"].HeaderText = "External IP";
                this.dataGridView1.Columns["macAddress"].HeaderText = "MAC Address";
                this.dataGridView1.Columns["ipSource"].HeaderText = "IP Source";
                this.dataGridView1.Columns["ipDest"].HeaderText = "IP Destination";
                this.dataGridView1.Columns["portSource"].HeaderText = "Source Port";
                this.dataGridView1.Columns["portDest"].HeaderText = "Destination Port";
                this.dataGridView1.Columns["protocol"].HeaderText = "Protocol";
                this.dataGridView1.Columns["label"].HeaderText = "Label";
                this.dataGridView1.Columns["country"].HeaderText = "Country";
                this.dataGridView1.Columns["state"].HeaderText = "State";
                this.dataGridView1.Columns["city"].HeaderText = "City";
                this.dataGridView1.Columns["packetCount"].HeaderText = "Packets";
            }
         }
         else
         {
          //handle if list is null
         }
        }