System.NullReferenceException. [英] System.NullReferenceException.

查看:91
本文介绍了System.NullReferenceException.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;

我在此代码行中收到上述异常.

DataGridView1.Rows[iIndex].Cells[1].Value = iGLCount.ToString();

datagridview是在Designer.cs中定义的,我认为我不需要故障排除提示使用''new''来创建对象实例"
在某些论坛上,我确实读过我无法直接修改/设置单元格,因此我应该启用编辑模式,如;

DataGridView1.BeginEdit();我得到了nada.

有任何想法吗?

Hi;

I get the above exception in this code line.

DataGridView1.Rows[iIndex].Cells[1].Value = iGLCount.ToString();

datagridview is defined in the Designer.cs i dont think i need the Troubleshooting tip "Use the ''new'' keyword to create an object instance"
in some forums i did read that i can not modify/set cells directly so i shoul enable the edit mode like ;

DataGridView1.BeginEdit(); and i got nada.

Any ideas ?

推荐答案

System.NullReferenceException
这仅表示您正在尝试使用实际上为NULL的对象的属性.


现在,看着你说的话:
System.NullReferenceException
This simply means that you are trying to use a property of an object which is actually a NULL.


Now, looking at the line you say:
DataGridView1.Rows[iIndex].Cells[1].Value = iGLCount.ToString();


可以轻松调试,查看和纠正的潜在对象是:
依次检查:
1. iGLCount:此对象为NULL吗?如果是这样,执行ToString()会引发错误
2. DataGridView1.Rows[iIndex]:这是否返回有效的行对象?如果是,请继续前进第三和最后一个
3. DataGridView1.Rows[iIndex].Cells[1]:这是否返回正确的对象?如果不是,并且为NULL,则执行.Value会引发错误.

现在,只需检查其中一个(1或2或3)并处理相同即可.使用前请先进行NULL检查.


Potential objects, which can be easily debugged, seen and rectified are:
Check sequentially:
1. iGLCount : Is this object NULL? If so, doing a ToString() would throw an error
2. DataGridView1.Rows[iIndex]: Does this return a valid row object? If yes, move ahead for third and last one
3. DataGridView1.Rows[iIndex].Cells[1] : Does this return a proper object? If not, and is a NULL, then doing a .Value would throw an error.

Now, just check which one of it is (1 or 2 or 3) and handle the same. Have a NULL check before using it.


这篇关于System.NullReferenceException.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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