为什么老是提示:未将对象引用设置到对象的实例

xiaojin0710 2009-01-07 01:48:39
在设计图中有个queryDataGridView,如果我实现在queryDataGridView里填充了数据的话,以下运行没有任何问题,可是如果我事先不在queryDataGridView里填充数据,以下代码就出现错误:未将对象引用设置到对象的实例。我该怎么写如下的if语句呢,请高手指点啊

If Not queryDataGridView.CurrentRow Is Nothing And File.Exists(Application.StartupPath & "\" & queryDataGridView.CurrentRow.Cells("编码").Value.ToString.Trim & "_2D.JPG") Then

.......

end if
...全文
122 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lunat 2009-01-07
  • 打赏
  • 举报
回复

'If Not queryDataGridView.CurrentRow Is Nothing And File.Exists(Application.StartupPath & "\" & queryDataGridView.CurrentRow.Cells("编码").Value.ToString.Trim & "_2D.JPG") Then

If (Not queryDataGridView.CurrentRow Is Nothing) AndAlso (Not queryDataGridView.CurrentRow.Cells("编码") Is Nothing) AndAlso (Not queryDataView.CurrentRow.Cells("编码").Value Is Nothing) AndAlso File.Exists(Application.StartupPath & "\" & queryDataGridView.CurrentRow.Cells("编码").Value.ToString().Trim & "_2D.JPG") Then


郁闷了 半年不用vb就生疏多了。。
LorenLiu 2009-01-07
  • 打赏
  • 举报
回复
那表示没有一个“编码”的Cell,就为null了。你得确保里面有这么一个Cell
xiaojin0710 2009-01-07
  • 打赏
  • 举报
回复
谢谢各位回复啊,好像这样写queryDataGridView.CurrentRow.Cells("编码") 就会提示没有对象引用,该怎么解决啊
lunat 2009-01-07
  • 打赏
  • 举报
回复
在c#里面用&&而不是&
vb里面记得也有个类似的短路与。。忘了怎么写的了
whitechololate 2009-01-07
  • 打赏
  • 举报
回复
Value.ToString 时 要判断Value值是否为null。再ToString()
你的代码是VB.net吧
xiaojin0710 2009-01-07
  • 打赏
  • 举报
回复
谢谢楼上回复,这个加上了,还是会出现同样的错误啊,怎么回事a,请高手指点
glt3260053 2009-01-07
  • 打赏
  • 举报
回复
未将对象引用设置到对象的实例
queryDataGridView没有取得值,值为空
hongqi162 2009-01-07
  • 打赏
  • 举报
回复
queryDataGridView.CurrentRow.Cells("编码") 判断一下这个是不是null才能去他的value

111,130

社区成员

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

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

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