Search code examples
c#.net-4.0visual-studio-2012nullreferenceexceptionvisual-studio-debugging

Why is it showing NullReferenceException when the variable is not null?


NRE

Why is VS 2012 showing the Type variable as an NullReferenceException when it also shows that it's value = "Retailer".

enter image description here

I've got a newborn, and I'm working on limited sleep, so I apologize if I'm missing something obvious here. The LoggedInUser.Employer object has been instantiated and this line works fine 1/2 of the time. But then it starts breaking. Not sure if this helps - need sleep...

 private string _type;
    public string Type
    {
        get { return _type; }
        set
        {
            if (value != null)
            {
                TypeEnum = (Constants.BusinessType)Enum.Parse(typeof(Constants.BusinessType), value, true);
                _type = value;
            }
        }
    }

enter image description here I'm starting to wonder if it's a cross-thread issue...

enter image description here


Solution

  • The ASP.NET ExecutionContext, responsible for storing the HttpContext.Current instance, won't naturally "flow" to other threads. Judging by your error stack trace, you're working in ASP.NET MVC, a framework that abstracts away the use of HttpContext. You've perhaps come from a WebForms background, where its direct use is common?

    SynchronizationContext

    This article offers much more detail than I can reasonably go into. Some of the points most relevant to your situation though are:

    "ExecutionContext is all about “ambient” information, meaning that it stores data relevant to the current environment or “context” in which you’re running."

    This "ambient" information being... HttpContext.Current and its various properties (including Session).

    "This means that this ambient context we’ve come to rely on for controlling details of our execution is no longer viable, because TLS doesn’t “flow” across these async points."

    TLS being thread-local-storage (HttpContext.Current, etc.) In short, async = potentially lose HttpContext.Current.

    The MVC way

    Remember I said MVC mostly abstracts away HttpContext?

    Session is in Controller.Session. (I'm sorry to say that as yet I have not tested this in an async controller action, so can't verify it's suitability for your needs as yet, or whether you will need additional work to make it cooperate.)

    Request is in Controller.Request

    User is in Controller.User

    There are others... check them out.

    Session Alternatives?

    Have you considered alternatives? You don't have to look far to find articles suggesting that Session + ASP.NET MVC is a bad idea. I'm not going to weigh in on something as generalised as whether or not it's a "bad thing", but looking at your example, it seems to me you're dealing with user profile data, not "session" data.

    Session isn't really the right place to be caching user profile information. For that matter, is it appropriate to cache it at all? Could a users profile change during a session? If they changed it themselves, would you reset the session? What if a separate admin user changed their profile while they were logged in?

    Exploring alternatives are beyond the scope of this question, but just be wary that you may be trying to solve the wrong problem here.