对象引用未设置为对象的实例---- ERROR

时间:2011-04-08 15:18:10

标签: asp.net vb.net

我收到以下错误: - 对象引用未设置为对象的实例 在下面的代码中: -

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim entityClassID As Integer
            Dim entityCode As Integer
            entityClassID = "3"
            entityCode = "44"


            If Int32.TryParse(Request("ec").ToString(), entityClassID) AndAlso 
              Int32.TryParse(Request("code").ToString(), entityCode) Then
              EntityClass.InnerText = entityClassID.ToString()
              EntityIdentifier.InnerText = entityCode.ToString()
            End If

            Dim da As New DataAccess()

            userID.InnerText = da.GetCurrentUserID()
            userName.InnerText = da.GetCurrentUserName()
        End If
    End Sub

da As New DataAccess()代码确实创建了一个实例,但是当da.GetCurrentUserID()时 被称为异常的方法如下: -

Line 27:             Dim da As New DataAccess()
Line 28: 
Line 29:             userID.InnerText = da.GetCurrentUserID()
Line 30:             userName.InnerText = da.GetCurrentUserName()

Source File: D:\Dev\workpackage\WebSites\Workpackage\customised\workpackage\Index.aspx.vb    Line: 29 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   UnionSquare.Workspace.Data.XmlParameteredSqlParser.ExecuteScalar() +100
   UnionSquare.Workspace.Data.XmlAccess.ExecuteScalar(String Source, XmlNode XmlQuery) +134
   UnionSquare.Workspace.Data.XmlAccess.ExecuteScalar(String Source) +39
   UnionSquare.Workspace.User.GetUserIDFromLogonUser(HttpContext context) +89
   UnionSquare.Workspace.User.get_CurrentID() +162
   UnionSquare.Workspace.User.get_Current() +38
   WorkPackageLibrary.WorkPackageLibrary.DataAccess.GetCurrentUserID() +70
   Index.Page_Load(Object sender, EventArgs e) in D:\Dev\workpackage\WebSites\Workpackage\customised\workpackage\Index.aspx.vb:29
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Thanx提前获得任何人可能提供的任何帮助

此致

Ghostrider

3 个答案:

答案 0 :(得分:1)

通过查看堆栈跟踪,您可以判断异常是从以下引发的:

UnionSquare.Workspace.User.GetUserIDFromLogonUser(HttpContext context) +89

您需要查看此来源。

P.S。 这与您的错误无关,但是...初始化entityClassID&的代码。 entityCode必须从string转换为int。删除引号会更有意义。您应该考虑在web.config中设置strict compilation option以防止此类事故发生。

Dim entityClassID As Integer
Dim entityCode As Integer
entityClassID = "3"
entityCode = "44"

应改为:

Dim entityClassID As Integer = 3
Dim entityCode As Integer = 44

答案 1 :(得分:0)

该错误似乎是值userNameuserIDNothing。在访问它们之前,您是否已确定它们已初始化?

答案 2 :(得分:0)

正如kd7指出的那样,错误可能来自da.GetCurrentUserID(),因为如果userID控件不存在,编译器会在构建时抱怨它。所以,检查一下这个方法。