对象引用未设置为对象的实例-读取dataGridView时 [英] Object reference not set to an instance of an object - When reading dataGridView

查看:131
本文介绍了对象引用未设置为对象的实例-读取dataGridView时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

尝试读取DataGridView中的单元格时出现问题.

当程序执行下面的代码时...

Hi all,

I am having a problem when trying to read cells in a DataGridView.

When the program executes the code below...

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (dataGridView1.Rows[i].Cells[0].Value.ToString().Length == 0 && dataGridView1.Rows[i].Cells[1].ToString().Length != 0)
    {
        MessageBox.Show("You must enter a description for each value.");
        return;
    }
    if (dataGridView1.Rows[i].Cells[1].Value.ToString().Length == 0 && dataGridView1.Rows[i].Cells[0].ToString().Length != 0)
    {
        MessageBox.Show("You must enter a value for each description.");
        return;
    }
}



出现以下错误:

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


我要做的是执行程序,然后在第一个字段[Description]中键入"test"(例如).当我单击激活验证的按钮时,会发生错误.

有人能帮我吗? :confused:

我正在使用Visual Studio Team System-C#

提前致谢! :)



The following error appears:

Object reference not set to an instance of an object.


What I do is execute the program and type ''test'' (for example), in the first field [Description]. When i Click on the button which activates the verification, the error happens.

Can someone help me? :confused:

I am using Visual Studio Team System - C#

Thanks in advance! :)

推荐答案

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


这是一个通用错误,当您要对其应用任何操作的任何未初始化或空值对象时,就会发生此错误.这就是为什么将其视为类型NullReferenceException的异常.

要克服此错误,请始终在对对象执行任何操作之前检查该对象上的"Null".

使用NullReferenceException和 [ ^ ]文章.


投票 接受答案 (如果有帮助的话).
Object reference not set to an instance of an object


It''s a generic error which occurs when you are having any uninitialized or null valued object for which you are going to apply any operations. That''s why It is considered as Exception of kind NullReferenceException.

To overcome this error always check ''Null'' on the object before any operation done on it.

You could have more clarity with NullReferenceException with THIS[^] article.


Please vote and Accept Answer if it Helped.


Don'不要使用具有索引的单元格,最好指定列名".

对Abhishek先生的一个小小的改动
Don''t Refer Cells With Index, Better to Specify Column Name''s.

A small change to Mr.Abhishek answer
dataGrid1.Rows[i].Cells["ColumnName"].Value != null


替换为您的代码:


Replace your code with:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
      if (!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Column1"].Value.ToString())&& !string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Column2"].Value.ToString())) //Above Statement is Used to Check For String is Null or Empty. By making use of string.IsnullorEmpty() We can Handle this kind of "Object Reference not set to instance of an object" situations. I am not sure how far My Code would be useful.. But have a look at this. 
       {
           if (dataGridView1.Rows[i].Cells["Column1"].Value.ToString().Length == 0
|| dataGridView1.Rows[i].Cells["Column1"].Value == null
&& dataGridView1.Rows[i].Cells[1].Value.ToString().Length != 0)
           {
                 MessageBox.Show("You must enter a description for each value.");
                 return;
           }
           if (dataGridView1.Rows[i].Cells["Column2"].Value.ToString().Length == 0 ||dataGridView1.Rows[i].Cells["Column2"].Value == null &&dataGridView1.Rows[i].Cells[0].Value.ToString().Length != 0)
           {
                 MessageBox.Show("You must enter a value for each description.");
                 return;
           }
     }
}



尝试此操作以查找您的错误:

Hi,
try this to find your error:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (dataGridView1.Rows[i].Cells[0] == null)
    {
        MessageBox.Show("Cells[0] is null");
        continue;
    }
    if (dataGridView1.Rows[i].Cells[1] == null)
    {
        MessageBox.Show("Cells[1] is null");
        continue;
    }
    if (dataGridView1.Rows[i].Cells[0].Value.ToString().Length == 0 && dataGridView1.Rows[i].Cells[1].ToString().Length != 0)
    {
        MessageBox.Show("You must enter a description for each value.");
        return;
    }
    if (dataGridView1.Rows[i].Cells[1].Value.ToString().Length == 0 && dataGridView1.Rows[i].Cells[0].ToString().Length != 0)
    {
        MessageBox.Show("You must enter a value for each description.");
        return;
    }
}


这篇关于对象引用未设置为对象的实例-读取dataGridView时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆