简体   繁体   中英

Object reference not set to an instance of an object

I am getting below error when i am trying do sorting. Object reference not set to an instance of an object.

public static string SortColumn    
{
  get
    {
      return HttpContext.Current.Session["SORT_COLUMN"].ToString();
    }
    set
    {
        HttpContext.Current.Session["SORT_COLUMN"] = value;
    }
}

please help me on this...

Session["SORT_COLUMN"] can return null and you can't call something on nothing , so ToString would fail.

Also, HttpContext.Current could return null , meaning you can't access Session - this can happen if you're trying to access the context from the global.asax code.

You have to initialize the Session variable before you access the getter. Otherwise you have to check:

return HttpContext.Current.Session["SORT_COLUMN"] != null ? 
       HttpContext.Current.Session["SORT_COLUMN"].ToString() : string.Empty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM