I have been experiencing a Object Reference not set to an instance of an object 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 NullReferenceException, and could not understand why, because it was working previously perfect without any issues.
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;
}
}
}
}
I have tried the following things that I encountered during my research:
1: "HttpContext.Current.Session" vs Global.asax "this.Session"
3: The Session object is null in ASP.NET MVC 4 webapplication once deployed to IIS 7 (W 2008 R2)
4: Log a user off when ASP.NET MVC Session expires
5: Login Session lost sometimes when redirect to action in asp.net mvc 3
None of the above worked for me.