简体   繁体   中英

NullReferenceException in static class

I have an asp.net application with a static "global" class. Inside of the static class I have a variable called user. When a user logs in a set that variable. Debugging through the log process. All my other static variable seems to be fine except two. I am not sure why I am getting 'GlobalVariables.User' threw an exception of type 'System.NullReferenceException'. Can anyone shed some light on this. Thanks

namespace GlobalClass

public static class GlobalVariables


public static User User { get; set; }

Its probably throwing because you didn't create an instance of User. One way to fix it is the following.

public static class GlobalVariables
{
   static GlobalVariables()
   {
       User = new User();
   }

   public static User User { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM