1

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 ?

3
  • Apparently, your userName session variable is null. It should crash when you initially attempt to assign userName to your text field. Is this where the behavior is occuring? You stated in your question, there are no session variables set at this point. Commented Jul 28, 2011 at 13:46
  • Are you sure that the Session object is null? Test your assumption. Commented Jul 28, 2011 at 13:49
  • Hey George, this is the thing that has been driving me crazy all days and simply cant understand. In the above script all my session variables are null and I know this. But as far as I can see the code goes into the if statement and because session is null hence Session("userId") Is Nothing is true, it should redirect to login. Why oh why, does it go beyond that attempt to attach the session "username" to the literal Commented Jul 28, 2011 at 13:49

3 Answers 3

1

From your code snippet it looks like the line Response.Write(Session("UID").ToString) will always be executed regardless of what happens with the if statement above it.

I wonder if the weird indentation isn't confusing you. Try looking at it like this:

If (Session("userName") IsNot Nothing) Then
    customerName.Text = Session("userName").ToString()
End If

Response.Write(Session("UID").ToString)

Notice that I aligned the End If with the corresponding If above and the Response.Write... as well. The Response.Write... line clearly sits outside of the If block and since there is not return or break or continue in the If block it will always get executed.

And btw, it's probably not the Session object that is null. You are calling ToString on an item you assume to be contained in the Session object. It's more likely that the Session does not contain a "UID" entry.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry see my edit, I posted the wrong code. I meant the shouldnt the redirect take care of the empty session why does it go beyond and attempt to load Session("userName")
0

Response.Write() is outside of the IF statement, so whether or not the Session Is Nothing, the Response.Write() method will be ran.


Alternatively, perhaps youre trying to assign a value to the Session if it doesnt already have one? In this case, I think you have your code in reverse:

customerName.Text = Session("userName").ToString()

Should be:

Session("userName") = customerName.Text

1 Comment

Sorry see my edit, I posted the wrong code. I meant the shouldnt the redirect take care of the empty session why does it go beyond and attempt to load Session("userName")
0

Your

customerName.Text = Session("userName").ToString();

will be executed no matter what the value of the session is. In the if condition, you are checking for the (Session("userId") and not Session("userName"). Both of them are different session variables. If for some reason Session("userName") value is not assigned prior, you will get the "Object reference not set to an instance of an object" exception. I would do a session null check first before assigning the value to customerName.Text

if(Session("userName") IsNot Nothing) Then
    customerName.Text = Session("userName").ToString();     

2 Comments

Vinay, thanks for replying. And yes I understand what you are saying. But lets say I have 10 User Session variables I dont want to go i.e. if(Session("userName") IsNot Nothing and if(Session("email") IsNot Nothing and if(Session("userId") IsNot Nothing etc.... I just thought If I check 1 of the session variables i.e. a strong one like userId I could simply check for this one to null no ?
Stevie, This might sound silly, but are you sure the session(userId) is null? I suppose the session("userId") is getting assigned some value somewhere. I would try the same thing with Session("UserName") and see if it is behaving the same way as session("userId").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.