0

I have a custom class User

public class User
{
    public string UserId { get; set; }
    public string LesseeCode { get; set; }
    public string ClientId { get; set; }
    public string VehicleId { get; set; }
    public string Status { get; set; }
    public string UnitDesc { get; set; }
    public string ItemId { get; set; }
    public string Category { get; set; }
    public string Type { get; set; }
    public double Amount { get; set; }
}

And when I try to define a new user here

public static User currentUser = (HttpContext.Current.Session["User"] == null) ? (User)HttpContext.Current.Session["User"] : new User();

I get a System.NullReferenceException error, I read that this is usually caused by a variable that is not nullable either not bing defined or being defined null , however based on how I defined currentUser there should be no way for it to be null?

Hamid Pourjam
  • 18,954
  • 8
  • 53
  • 67
  • you need to change == to != it looks you mistakenly put == – Ehsan Sajjad Aug 06 '15 at 16:04
  • 1
    Because HttpContext.Current is null... You can't use it in a static initializer, it's not populated until a page request happens when the app initializes. It also only has a value in a web app (with a web.config). If you have code that relies on HttpContext.Current is should happen during or after Application_Init. HttpContext has a request and response object, so in order to be valid it has to be accessed during an Http Request, after all the modules have initialized. You can however, fake an HttpContext by setting one yourself. – Ryan Mann Aug 06 '15 at 16:08
  • and I get downvoted because I'm telling the truth! bad world! :D @Ryios – Hamid Pourjam Aug 06 '15 at 16:13
  • This code is also not thread safe. Storing the current user to a static variable won't work. Your current user will always be the first user to hit the site, and the next person will get the same current user object. You could mark it with a threadstatic attribute... but it's better to just pull your current user from HttpContext.Current.Request.User and let it handle it for you. – Ryan Mann Aug 06 '15 at 16:15

2 Answers2

0

Because currentUser is static and somehow it's value is getting evaluated before HttpContext.Current.Session exists!

You should also change == to !=

currentUser = (HttpContext.Current.Session["User"] != null) ? (User)HttpContext.Current.Session["User"] : new User();

Or change the order of 2nd and 3rd operand

currentUser = (HttpContext.Current.Session["User"] == null) ? new User() : (User)HttpContext.Current.Session["User"];
Hamid Pourjam
  • 18,954
  • 8
  • 53
  • 67
0

You need to either reverse your true and false case results, or inverse the operator. I think the easier of the two is to change your == to !=.

public static User currentUser = (HttpContext.Current.Session["User"] == null) ? (User)HttpContext.Current.Session["User"] : new User();
                                               you want this to be != ^^

Currently you are saying

If HttpContext.Current.Session["User"] is null then assign it to currentUser, otherwise create a new user.

So you'll always either have a null user or a new user.

Will Custode
  • 4,404
  • 2
  • 21
  • 49