最简单的if语句出错了,不知道为什么

zhengyingcan 2009-06-19 06:58:07
if (dataGridView1.CurrentRow.Cells[0].Value == null )
{
MessageBox.Show("导体没有数据,不能保存");
return;
}
这样子在运行的时候是没有问题的,但是我改成下面代码
if (dataGridView1.CurrentRow.Cells[0].Value == null && dataGridView2.CurrentRow.Cells[0].Value == null)
{
MessageBox.Show("导体没有数据,不能保存");
return;
}
就报错了,提示居然是 未将对象引用设置到对象的实例,不知道为什么.
...全文
88 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanshiwu0620 2009-06-20
  • 打赏
  • 举报
回复
最佳解决方案 if (dataGridView1.CurrentRow.Cells.Count==0 || dataGridView1.CurrentRow.Cells[0].Value == null )
modbus999 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 athwind 的回复:]
引用楼主 zhengyingcan 的帖子:
if (dataGridView1.CurrentRow.Cells[0].Value == null )
{
MessageBox.Show("导体没有数据,不能保存");
return;
}
这样子在运行的时候是没有问题的,但是我改成下面代码
if (dataGridView1.CurrentRow.Cells[0].Value == null && dataGridView2.CurrentRow.Cells[0].Value == null)
{
MessageBox.Show("导体没有数据,不能…

LZ没有弄明白值类型,以及引用类型之间的比较(其实好多人都…
[/Quote]
高手
newdigitime 2009-06-20
  • 打赏
  • 举报
回复
mark,学习了.
binhu12332100 2009-06-20
  • 打赏
  • 举报
回复
22楼说得对……
燃烧的荷尔蒙 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ljhcy99 的回复:]
因为
dtaGridView2.CurrentRow,
dataGridView1.CurrentRow
俩个CurrentRow不能同时不为null
[/Quote]
回帖是一种美德!
DSIOF3KIDSKTR 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhengyingcan 的帖子:]
if (dataGridView1.CurrentRow.Cells[0].Value == null )
{
MessageBox.Show("导体没有数据,不能保存");
return;
}
这样子在运行的时候是没有问题的,但是我改成下面代码
if (dataGridView1.CurrentRow.Cells[0].Value == null && dataGridView2.CurrentRow.Cells[0].Value == null)
{
MessageBox.Show("导体没有数据,不能…
[/Quote]
LZ没有弄明白值类型,以及引用类型之间的比较(其实好多人都不太明白),常用的比较运算符有:==,Equals(),ReferenceEquals()
在判断引用类型是否为null时尽量不要用'==',当你所要进行比较的value还没有初始化(内存中还没分配地址,这和值类型有点不同)的时候,将value与null进行比较会报错,因为value现在连null都算不上
解决方法:
if (ReferenceEquals(dataGridView1.CurrentRow.Cells[0].Value,null) && ReferenceEquals(dataGridView2.CurrentRow.Cells[0].Value,null))
……
javachenheng 2009-06-19
  • 打赏
  • 举报
回复
应该是没有实例化或没有赋值出现这个错误提示,你的应该是没有实例化,断点调试试试,没必要发帖子问人的!
helloneo 2009-06-19
  • 打赏
  • 举报
回复
同意11楼的看法,把&&改为||再测试
ld1201 2009-06-19
  • 打赏
  • 举报
回复
if (dataGridView1.CurrentRow.Cells.Count==0 || dataGridView1.CurrentRow.Cells[0].Value == null )
kingchonlim 2009-06-19
  • 打赏
  • 举报
回复
dataGridView2.CurrentRow.Cells[0].Value这句问题.
windinwing 2009-06-19
  • 打赏
  • 举报
回复
上面换成||
windinwing 2009-06-19
  • 打赏
  • 举报
回复
dataGridView1.CurrentRow.Cells.Count>0 用这个判断
实在不行
if (dataGridView1.CurrentRow== null && dataGridView1.CurrentRow.Cells.Count == 0)
试试这样
new_era 2009-06-19
  • 打赏
  • 举报
回复
楼主自己解决了,恭喜
zhengyingcan 2009-06-19
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ljhcy99 的回复:]
因为
dtaGridView2.CurrentRow,
dataGridView1.CurrentRow
俩个CurrentRow不能同时不为null
[/Quote]
正确答案跟这个接近,只要同时选中两个datagridview就不会出错.因为两个必须在选中的前题下才有用.
normalpeople 2009-06-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mastzou 的回复:]
从测试的角度建议:
楼主测试以下代码:
bool g1=dataGridView1.CurrentRow.Cells[0].Value == null;
bool g2=dataGridView2.CurrentRow.Cells[0].Value == null;
if(g1&&g2)
{
xxxxxxxxxxxxxx
}
[/Quote]
up,这样测试下就知道问题了,或者重启下vs
ljhcy99 2009-06-19
  • 打赏
  • 举报
回复
因为
dtaGridView2.CurrentRow,
dataGridView1.CurrentRow
俩个CurrentRow不能同时不为null
让爱延续 2009-06-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mastzou 的回复:]
从测试的角度建议:
楼主测试以下代码:
bool g1=dataGridView1.CurrentRow.Cells[0].Value == null;
bool g2=dataGridView2.CurrentRow.Cells[0].Value == null;
if(g1&&g2)
{
xxxxxxxxxxxxxx
}
[/Quote]

好专业(⊙o⊙)哦! lz给分吧!!!
andy0618 2009-06-19
  • 打赏
  • 举报
回复
并不是IF语句有问题,而是
dataGridView1.CurrentRow.Cells[0]或者dataGridView2.CurrentRow.Cells[0]没有,或者没有实例化
mastzou 2009-06-19
  • 打赏
  • 举报
回复
还建议,重新启动VS,重新启动计算机
mastzou 2009-06-19
  • 打赏
  • 举报
回复
从测试的角度建议:
楼主测试以下代码:
bool g1=dataGridView1.CurrentRow.Cells[0].Value == null;
bool g2=dataGridView2.CurrentRow.Cells[0].Value == null;
if(g1&&g2)
{
xxxxxxxxxxxxxx
}
加载更多回复(6)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧