ToxicPineapple
ToxicPineapple

Reputation: 88

C# ASP.NET Session Manager throwing null reference exception

I am trying to implement a static SessionManager class that is supposed to act as a wrapper around a SessionStore object, which is stored in HttpContext.Current.Session["objSession"] and actually holds all of the user's session data. The SessionManager class has identical properties as SessionStore, but has extra methods needed to manipulate the session data as needed. Basically, the SessionManager facilitates getting/setting properties stored in the session object.

All classes are stored in the same namespace as the web solution, and all are serializable.

I have tried two different solutions to my problem, both threw a null reference exception at the same point, when trying to do ANYTHING with HttpContext.Current.Session:

public static class SessionManager
    {
        static SessionManager()
        {
            if (HttpContext.Current.Session != null)
            {
                try
                {
                    if (HttpContext.Current.Session["objStore"] == null)
                    {
                        HttpContext.Current.Session["objStore"] = new SessionStore();
                    }
                }
                catch (NullReferenceException)
                {
                    HttpContext.Current.Session["objStore"] = new SessionStore();
                }
            }
        }

Code-behind of the calling page:

protected void Page_Load(object sender, EventArgs e)
        {

            if (SessionManager.groupSettings.Count > 0)
            {
                pnlDashboard.Visible = true;
                pnlLogin.Visible = false;
                getDisplayData();
            }
            else
            {
                pnlDashboard.Visible = false;
                pnlLogin.Visible = true;
            }
        }

The debugger steps into SessionManager all the way down to line

if (HttpContext.Current.Session != null)

where it then stops and throws the exception. However, when I hover over the code and the properties dialog opens, it shows that the HttpContext.Current.Session object is NOT null. The resulting call stack is here, but indicates that the source line is if (SessionManager.groupSettings.Count > 0), which is in the code-behind:

[NullReferenceException: Object reference not set to an instance of an object.]
   Project.Default.Page_Load(Object sender, EventArgs e) in C:\Users\ASP\Project\Project\Default.aspx.cs:20
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2952

My second attempt had all of the code within the static constructor above, but had it in a public static sessionStart() method, which was called above the first if statement in the calling page's code-behind:

protected void Page_Load(object sender, EventArgs e)
    {
        SessionManager.sessionStart()
        if (SessionManager.groupSettings.Count > 0)
        {
            pnlDashboard.Visible = true;
            pnlLogin.Visible = false;
            getDisplayData();
        }
        else
        {
            pnlDashboard.Visible = false;
            pnlLogin.Visible = true;
        }
    }

I am really stumped as to what could be causing this problem. I have static classes elsewhere in my code and haven't had any issues, and the Session seems to not be null.

I appreciate all help. Thank you!

Upvotes: 0

Views: 1212

Answers (1)

ToxicPineapple
ToxicPineapple

Reputation: 88

So it seems I did not initialize some objects within my SessionStore class, because I added a constructor that initialized them, and the problem is now fixed. Maybe the problem was actually happening when the object was being serialized (as is the case when an object is stored into the stateserver), and the error message confused me.

Edit - I always do this... Figure out the solution AFTER I've posted to StackOverflow... :(

Upvotes: 1

Related Questions