0

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!

2
  • SessionManager.groupSettings? What is that? Commented Apr 23, 2016 at 22:27
  • A List<GroupSettings> and actually seems to be the cause of my problems... I initialized all properties within SessionStore and it seems that the problem is fixed (perhaps it can't serialize null objects?). I'm not encountering the error anymore and the session seems to be maintaining all values. My next issue is that the debugger now treats "step over" as "Continue." Odd. Commented Apr 23, 2016 at 22:47

1 Answer 1

1

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... :(

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.