4

I have a List of objects stored in a session variable. But when I check to make sure the session variable is not null, it gives me a NullReferenceException: Object reference not set to an instance of an object.

if(Session["object"] == null)  //error occurs here
{
     Session["object"] = GetObject();
     return Session["object"] as List<object>;
}
else
{
    return Session["object"] as List<object>;
}

How can I check if the Session is null?

edit: I also tried

if(Session["object"] != null)
6
  • 2
    Have you checked that Session is not null? Commented Jan 29, 2014 at 14:03
  • 1
    Are you sure that your error occurs within in the line with your if-condition? Commented Jan 29, 2014 at 14:04
  • 2
    Which object is null? Based on the description, it sounds like Session itself is null, which would mean that there is no Session in this context. Commented Jan 29, 2014 at 14:05
  • 2
    Where you have this code ? is it in a code behind of web page ? or is it part of web service ? ?? Commented Jan 29, 2014 at 14:06
  • Put a breakpoint on the IF ad look at the session contents. If its null and you dont expect it to be, then debug from there. Commented Jan 29, 2014 at 14:10

4 Answers 4

7

edit: I also tried

if(Session["object"] != null)

Note that the code in your edit is not checking that Session is not null. If it is null, the expression Session["object"] will still cause the error because this is searching the session instance for the "object" key - so if you think about it logically you are looking for a key in an uninstantiated object, hence the null reference exception. Therefore you need to verify Session has been instantiated before checking if Session["object"] is null:

if (Session != null)
{
    if(Session["object"] == null)
    {
         Session["object"] = GetObject();
         return Session["object"] as List<object>;
    }
    else
    {
        return Session["object"] as List<object>;
    }
}
else
{
    return null;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hopefully we will see monadic null checking implemented in C# 6.0 to simplify this. :)
0

have you check your GetObject() method...it may return null...

Comments

0

Have you checked if Session != null? In your example, both this.Session and this.Session["Key"] can return null, but you checked only the latter. Take into account the option that the Session itself might not exist, so you cannot get data from it, but also cannot write to it.

3 Comments

@Mike: Define "I have" and "didn't seem to work." According to your description, Session itself appears to be null. What did you try? How did it not work? What are you attempting to accomplish here?
by the way this should be a comment not an answer
Checking if Session is null is at least part of the solution. I have had this same problem. Session can be null depending on config/settings of your system, or if you're deep inside some module that doesn't support Session, yet the namespace is still available.
0

Try checking if (!Session.ContainsKey("object")) after checking if (Session != null). If those checks both pass, then Session["object"] should be fine.

Comments

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.