forum

Home / DeveloperSection / Forums / Checking if session variable is null returns nullreferenceexception

Checking if session variable is null returns nullreferenceexception

Anonymous User 3328 29-Jan-2014

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?

I also tried

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


c# c# 
Updated on 29-Jan-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By