Hey Just wondering how the following can happen
Object reference not set to an instance of an object.
customerName.Text = Session(“userName”).ToString()
If (Session("userId") Is Nothing) Then
Response.Redirect(ConfigurationManager.AppSettings("site_base_url").ToString & "login/", False)
End If
customerName.Text = Session("userName").ToString()
Now I currently have no session set on this page. So the session is NULL but i am just wondering why my if statement is taking care of this problem, why does it even try to get to the next part i.e. Response.Write ?
From your code snippet it looks like the line
Response.Write(Session("UID").ToString)will always be executed regardless of what happens with theifstatement above it.I wonder if the weird indentation isn’t confusing you. Try looking at it like this:
Notice that I aligned the
End Ifwith the correspondingIfabove and theResponse.Write...as well. TheResponse.Write...line clearly sits outside of theIfblock and since there is not return or break or continue in theIfblock it will always get executed.And btw, it’s probably not the
Sessionobject that is null. You are callingToStringon an item you assume to be contained in the Session object. It’s more likely that the Session does not contain a “UID” entry.