HttpContext.Current.Session - NullReferenceException

Hennie

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"

2: Rare System.NullReferenceException show up for a HttpContext.Current.Session["courseNameAssociatedWithLoggedInUser"] that was previously populated?

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.

mybirthname
        public static Settings Current
        {
            get
            {
                if(HttpContext.Current.Session == null)
                {
                     Settings s = new Settings();
                     return s;
                }
                if (HttpContext.Current.Session["Settings"] == null)
                {
                    Settings s = new Settings();
                    HttpContext.Current.Session["Settings"] = s;
                    return s;
                }

                return (Settings)HttpContext.Current.Session["Settings"];
            }
        }

You are wrapping the Session["Settings"] to Setting class when it is null. If you change the code like this it should work.

Learn to use debug in the future, you will resolve errors like this really fast !

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

OWIN and HttpContext.Current.Session

From Dev

how to use session with HttpContext.Current at null

From Dev

HttpContext.Current.Session Always Null

From Dev

HttpContext.Current.Session is null + OWIN

From Dev

HttpContext.Current.Session Auto Expiration?

From Dev

What's the difference between HttpContext.Current.Session and HttpContext.Current.Items?

From Dev

Why is HttpContext.Current null during the Session_End event?

From Dev

How do I access HttpContext.Current.Session in a class library?

From Dev

Is it possible to access HttpContext.Current.Session from Web API

From Dev

Keep HttpContext.Current.Session in WCF service to service call

From Dev

Is HttpContext.Current.Session shared between worker processes?

From Dev

Can't retrieve the value in HttpContext.Current.Session state

From Dev

if (HttpContext.Current.Session["id"] != null) is returning NULL EXCEPTION

From Dev

Session Manager will not log me out when session expires, HTTPContext.Current is Null

From Dev

ASP.NET: The difference between page.Session and HttpContext.Current.Session

From Dev

HttpContext.Current.Session is null when injecting it to interceptor / or using in inside interceptor (mvc4 webapi)

From Dev

Why is HTTPContext.Current.Session null using SignalR 2.x libraries in a ASP .Net MVC application?

From Dev

Does "HttpContext.Current.User.Identity.Name" returns null if session times out?

From Dev

Does "HttpContext.Current.User.Identity.Name" returns null if session times out?

From Dev

Httpcontext current cache gets cleared

From Dev

Understanding HttpContext.Current.Cache

From Dev

Why is HttpContext.Current null?

From Dev

Why HttpContext.Current is Static?

From Dev

Access the current HttpContext from a ILogger

From Dev

HttpContext.Current Null in CacheItemUpdateCallback

From Dev

Mocking HttpContext.Current.Application

From Dev

Alternative to HttpContext.Current.Items

From Dev

difference between HttpContext.Session and HttpContext.Application

From Dev

Why is HttpContext.Session status changing to disposed?

Related Related

  1. 1

    OWIN and HttpContext.Current.Session

  2. 2

    how to use session with HttpContext.Current at null

  3. 3

    HttpContext.Current.Session Always Null

  4. 4

    HttpContext.Current.Session is null + OWIN

  5. 5

    HttpContext.Current.Session Auto Expiration?

  6. 6

    What's the difference between HttpContext.Current.Session and HttpContext.Current.Items?

  7. 7

    Why is HttpContext.Current null during the Session_End event?

  8. 8

    How do I access HttpContext.Current.Session in a class library?

  9. 9

    Is it possible to access HttpContext.Current.Session from Web API

  10. 10

    Keep HttpContext.Current.Session in WCF service to service call

  11. 11

    Is HttpContext.Current.Session shared between worker processes?

  12. 12

    Can't retrieve the value in HttpContext.Current.Session state

  13. 13

    if (HttpContext.Current.Session["id"] != null) is returning NULL EXCEPTION

  14. 14

    Session Manager will not log me out when session expires, HTTPContext.Current is Null

  15. 15

    ASP.NET: The difference between page.Session and HttpContext.Current.Session

  16. 16

    HttpContext.Current.Session is null when injecting it to interceptor / or using in inside interceptor (mvc4 webapi)

  17. 17

    Why is HTTPContext.Current.Session null using SignalR 2.x libraries in a ASP .Net MVC application?

  18. 18

    Does "HttpContext.Current.User.Identity.Name" returns null if session times out?

  19. 19

    Does "HttpContext.Current.User.Identity.Name" returns null if session times out?

  20. 20

    Httpcontext current cache gets cleared

  21. 21

    Understanding HttpContext.Current.Cache

  22. 22

    Why is HttpContext.Current null?

  23. 23

    Why HttpContext.Current is Static?

  24. 24

    Access the current HttpContext from a ILogger

  25. 25

    HttpContext.Current Null in CacheItemUpdateCallback

  26. 26

    Mocking HttpContext.Current.Application

  27. 27

    Alternative to HttpContext.Current.Items

  28. 28

    difference between HttpContext.Session and HttpContext.Application

  29. 29

    Why is HttpContext.Session status changing to disposed?

HotTag

Archive