title: Set and get session values in HttpContext.Current.Session in C#
body: <p>I have static class called CommoonHelper.</p>
  
  <pre><code> public static  class CommonHelper
      {
  
          public static SessionObjects sessionObjects
          {
              get
              {
  
                  if ((HttpContext.Current.Session["sessionObjects"] == null))
                  {
  
                      return null;
                  }
                  else
                  {
                      return HttpContext.Current.Session["sessionObjects"] as SessionObjects;
                  }
              }
              set {
                  HttpContext.Current.Session["sessionObjects"] = value;
              }
          }
  
      }
  </code></pre>
  
  <p>In SessionObjects Class, I have defined properties for get /set like below.</p>
  
  <pre><code> public class SessionObjects
      {
          public  int UserId { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public string UserName { get; set; }
          public string DisplayName
          {
              get
              {
                return  FirstName + "" + LastName;
              }
          }
      }
  </code></pre>
  
  <p>When i try to assign an value like below.</p>
  
  <pre><code>CommonHelper.sessionObjects.LastName = "test";
  </code></pre>
  
  <p>Its throwing the below exception.</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
  </code></pre>
  
  <p>How do i fix this ?</p>