对象引用未设置为对象上的实例错误 [英] object reference not set to instance on an object Error

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

问题描述

Private Sub bind()
        Try
            Dim _objDA As New DataAccess()
            Dim _dsRubricPCGDetails As DataSet = _objDA.GetRubricAndPCGDetails()
            Dim dt As DataTable = _dsRubricPCGDetails.Tables(0)

            Dim ddlsampleLocal As DropDownList =    TryCast(FindControl("DropDownList2"), DropDownList)

            ddlsampleLocal.DataSource = dt
            ddlsampleLocal.DataValueField = "Domain_Value_ID"
            ddlsampleLocal.DataTextField = "Description"
            ddlsampleLocal.DataBind()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

............................................ ....
ddlsampleLocal.DataSource = dt ---->在此行中,我正在获取此对象引用未设置为对象错误实例".
有人可以帮我吗?

................................................
ddlsampleLocal.DataSource = dt ---->in this line Im getting this "object reference not set to instance on an object Error".
Can any one help me

推荐答案

当您尝试使对象为空时,会发生此错误.

确保:
Dim dt As DataTable = _dsRubricPCGDetails.Tables(0)
确实返回一个表,并且dt在此行之后不再为null.

调试将对您有很大帮助.
This error happens when you try to null object.

Make sure:
Dim dt As DataTable = _dsRubricPCGDetails.Tables(0)
does return a table and dt is no more null after this line.

DEBUGGER would be of great help, try it.


在我看来,ddlsampleLocal 为null.
TryCast(FindControl("DropDownList2"), DropDownList)行似乎返回null.

就像Sandeep所说的那样,尝试调试代码.
To me it looks like ddlsampleLocal is null.
The line TryCast(FindControl("DropDownList2"), DropDownList) appears to be returning null.

Just like Sandeep said, try debugging your code.


Dim dt As DataTable = _dsRubricPCGDetails.Tables(0)

我调试了它,是的_dsRubricPCGDetails.Tables(0)将表分配给dt.
在调试时,我也看到了Tabke.但是仍然会出现相同的错误.


在具有相同代码的一页中,我得到了数据.所以我在第二页中复制了相同的代码.但是在第二页中,我遇到了这个错误.
Dim dt As DataTable = _dsRubricPCGDetails.Tables(0)

I debugged it and yes _dsRubricPCGDetails.Tables(0) is assigning a table to dt.
While debuging I saw the tabke also..But still same error is coming.


In one page with the same code I got the data. So I coppied the same code in 2nd page.But in this 2nd page I am getting this error.


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

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