I am getting the following error :- Object reference not set to an instance of an object in the following piece of code :-
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
The da As New DataAccess() code does create an instance, but when da.GetCurrentUserID() is called an exception is thrown as follows :-
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(开发者_如何学Go) +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
Thanx in advance for any help that anyone might be able to provide
Regards
Ghostrider
By looking at the stack trace you can tell that the exception is thrown from:
UnionSquare.Workspace.User.GetUserIDFromLogonUser(HttpContext context) +89
You need to look at this source.
P.S. This is unrelated to your error but... The code where you initialize entityClassID & entityCode has to convert from string to int. It would make more sense to remove the quotes. You should look into setting strict compilation option in your web.config to prevent accidents like this.
Dim entityClassID As Integer
Dim entityCode As Integer
entityClassID = "3"
entityCode = "44"
Should be changed to:
Dim entityClassID As Integer = 3
Dim entityCode As Integer = 44
The bug appears to be that the value userName
or userID
are Nothing
. Have you made sure that they are initialized before accessing them here?
As kd7 pointed out, the error is probably coming from da.GetCurrentUserID()
, since if userID
control does not exists the compiler would complain about it at build time. So, check that method.
精彩评论