Thursday 6 September 2012

Object reference not set to an instance of an object exception with accessing session variable

The "Object reference not set to an instance of an object" exception usually happens when you try to address an object that has not been set.

You can avoid the "Object reference not set to an instance of an object" exception  by checking each object to make sure it is a valid object before attempting to access it.

Example :

if (Session["VeriableName"] != null)
  {
     //Access the session value
  }


When they say "initialize" the session they mean setting an intial value for a session variable (not like initializing a class). the best place to do it is Session_Start though you can do it anywhere.

No comments:

Post a Comment