title: Asp.net MVC 5, Identity 2.0 Unable to add a Role to user
body: <p>i am trying to add a role to a specific user using controllers. But, i am getting error</p>
  
  <p>Here my Role Controller:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult RoleAddToUser(string UserName, string RoleName)
  {
      ApplicationUser user = context.Users.Where(u =&gt; u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
      var account = new AccountController();
      account.UserManager.AddToRole(user.Id, RoleName);
  
      ViewBag.ResultMessage = "Role created successfully !";
  
      // prepopulat roles for the view dropdown
      var list = context.Roles.OrderBy(r =&gt; r.Name).ToList().Select(rr =&gt; new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
      ViewBag.Roles = list;
  
      return View("ManageUserRoles");
  }    
  </code></pre>
  
  <p>Here is my Action Controller:</p>
  
  <pre><code>public class AccountController : Controller
  {
      private ApplicationUserManager _userManager;
  
      public AccountController()
      {
      }
  
      public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager )
      {
          UserManager = userManager;
          SignInManager = signInManager;
      }
  
      public ApplicationUserManager UserManager
      {
          get
          {
              return _userManager ?? HttpContext.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;();
          }
          private set
          {
              _userManager = value;
          }
      }
  </code></pre>
  
  <p>This is the error i am getting:</p>
  
  <p>Server Error in '/' Application.</p>
  
  <p>Object reference not set to an instance of an object.</p>
  
  <p>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
  
  <p>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.</p>
  
  <p>Source Error: </p>
  
  <pre><code>Line 32:             get
  Line 33:             {
  Line 34:                 return _userManager ??      HttpContext.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;();
  Line 35:             }
  Line 36:             private set
  </code></pre>