title: HttpContext.Current.Session - NullReferenceException
body: <p>I have been experiencing a <strong>Object Reference not set to an instance of an object</strong> error in my MVC 4 ASP.NET project's Settings class, that gets my Current Session details.  Each time I browse a page the variable throws the <strong>NullReferenceException</strong>, and could not understand why, because it was working previously perfect without any issues.</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace TracerCRM.Web
  {
      public class Settings
      {
          public static Settings Current
          {
              get
              {
                  Settings s = HttpContext.Current.Session["Settings"] as Settings;
                  if (s == null)
                  {
                      s = new Settings();
                      HttpContext.Current.Session["Settings"] = s;
                  }
  
                  return s;
              }
          }
      }
  }
  </code></pre>
  
  <p>I have tried the following things that I encountered during my research:</p>
  
  <p>1: <a href="https://stackoverflow.com/questions/464456/httpcontext-current-session-vs-global-asax-this-session">&quot;HttpContext.Current.Session&quot; vs Global.asax &quot;this.Session&quot;</a></p>
  
  <p>2: <a href="https://stackoverflow.com/questions/15483170/rare-system-nullreferenceexception-show-up-for-a-httpcontext-current-sessionco">Rare System.NullReferenceException show up for a HttpContext.Current.Session[&quot;courseNameAssociatedWithLoggedInUser&quot;] that was previously populated?</a></p>
  
  <p>3: <a href="https://stackoverflow.com/questions/17947975/the-session-object-is-null-in-asp-net-mvc-4-webapplication-once-deployed-to-iis">The Session object is null in ASP.NET MVC 4 webapplication once deployed to IIS 7 (W 2008 R2)</a></p>
  
  <p>4: <a href="https://stackoverflow.com/questions/25541704/log-a-user-off-when-asp-net-mvc-session-expires">Log a user off when ASP.NET MVC Session expires</a></p>
  
  <p>5: <a href="https://stackoverflow.com/questions/18049865/login-session-lost-sometimes-when-redirect-to-action-in-asp-net-mvc-3">Login Session lost sometimes when redirect to action in asp.net mvc 3</a></p>
  
  <p>None of the above worked for me.</p>