System.NullReferenceException:未将对象引用设置为对象的实例。 [英] System.NullReferenceException: Object reference not set to an instance of an object.

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

问题描述



执行此代码时为什么会出现上述错误消息:

Public Sub DropDown_SelectedIndexChanged(ByVal sender As System.Object,

ByVal e As System.EventArgs)

Dim list As DropDownList = CType(sender,DropDownList)

Dim cell = CType(list.Parent,TableCell)

Dim item = CType(cell.Parent,DataGridItem)

Dim index = item.ItemIndex


Dim ds As DataSet

ds = DataGrid1.DataSource


Dim row As DataRow

row = ds.Tables(0).Select(" ID = ''" + item.Cells(0).Text +"''")(0)

''< --- ERROR

End Sub

解决方案

在线上设置断点


row = ds.Tables(0).Select (" ID =''" + item.Cells(0).Text +"''")(0)


并查看那里的null。


可能是:

ds中没有表格;

否单元格中的单元格(o);

没有项目;

选择不返回任何内容

....


调试它。


Eliyahu


" John Smith" <乔******** @ microsoft.com>在消息中写道

新闻:%Z ******************** @ news.siol.net ...


为什么在执行此代码时会出现上述错误消息:

Public Sub DropDown_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Dim cell = CType(list.Parent,TableCell)
Dim item = CType(cell.Parent,DataGridItem)
Dim index = item.ItemIndex

Dim ds As DataSet
ds = DataGrid1.DataSource

Dim row As DataRow
row = ds.Tables(0).Select (" ID =''" + item.Cells(0).Text +"''")(0)
''< --- ERROR
End Sub




谢谢。


" ds"是空的。看来我需要以某种方式将DataSource添加到Session对象

中。初学者的错误。似乎DataGrid1.DataSource不是持久性的?


" ds"是空的。看来我需要以某种方式将DataSource添加到Session对象中。初学者的错误。似乎DataGrid1.DataSource不是持久的吗?




我已经将ds声明为全局共享变量,它到目前为止工作正常。



Why do I get above error message while executing this code:

Public Sub DropDown_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Dim list As DropDownList = CType(sender, DropDownList)
Dim cell = CType(list.Parent, TableCell)
Dim item = CType(cell.Parent, DataGridItem)
Dim index = item.ItemIndex

Dim ds As DataSet
ds = DataGrid1.DataSource

Dim row As DataRow
row = ds.Tables(0).Select("ID = ''" + item.Cells(0).Text + "''")(0)
''<--- ERROR
End Sub

解决方案

Set a break point on the line

row = ds.Tables(0).Select("ID = ''" + item.Cells(0).Text + "''")(0)

and see what is null there.

Could be:
no tables in ds;
no Cells(o) in item;
no item;
select doesn''t return anything
....

Debug it.

Eliyahu

"John Smith" <jo********@microsoft.com> wrote in message
news:%Z********************@news.siol.net...


Why do I get above error message while executing this code:

Public Sub DropDown_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Dim list As DropDownList = CType(sender, DropDownList)
Dim cell = CType(list.Parent, TableCell)
Dim item = CType(cell.Parent, DataGridItem)
Dim index = item.ItemIndex

Dim ds As DataSet
ds = DataGrid1.DataSource

Dim row As DataRow
row = ds.Tables(0).Select("ID = ''" + item.Cells(0).Text + "''")(0)
''<--- ERROR
End Sub




Thank you.

"ds" was null. It seems I need to add DataSource into Session object
somehow. Beginner mistake. It seems DataGrid1.DataSource is not persistance?


"ds" was null. It seems I need to add DataSource into Session object
somehow. Beginner mistake. It seems DataGrid1.DataSource is not
persistance?



I''ve declared ds as global Shared variable and it works fine so far.


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

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