0

I'm extending the ApplicationUser class with some extra information and relationships that I need to use in my application.

Here's my ApplicationUser class:

public class ApplicationUser : IdentityUser
{
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Display(Name = "Full Name")]
    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
    }

    [Display(Name = "Job Title")]
    public string JobTitle { get; set; }

    public virtual ICollection<Permission> Permissions { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        userIdentity.AddClaim(new Claim("FullName", this.FullName));

        return userIdentity;
    }
}

My Permission model:

public class Permission : BaseEntity
{
    public int ID { get; set; }
    public string Name { get; set; }

    // Many to One
    public virtual ICollection<ApplicationUser> Users { get; set; }
}

And my Create() method in my UsersController:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(CreateUserViewModel model)
{
    var selectedPermissions = model.Permissions.Where(x => x.IsChecked).Select(x => x.ID).ToList();

    if (ModelState.IsValid)
    {
        var user = new ApplicationUser {
            UserName = model.Email,
            Email = model.Email,
            PrivateEmail = model.PrivateEmail,
            FirstName = model.FirstName,
            LastName = model.LastName,
            JobTitle = model.JobTitle
        };


        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            // Remove all existing Roles
            var userRoles = await UserManager.GetRolesAsync(user.Id);
            var uRoles = String.Join(", ", userRoles.ToArray());
            await UserManager.RemoveFromRolesAsync(user.Id, uRoles);

            // Add selected Role
            await UserManager.AddToRoleAsync(user.Id, model.Role);

            foreach (var permissionID in selectedPermissions)
            {
                var permission = await db.Permissions.FindAsync(permissionID);
                if (permission != null)
                {
                    user.Permissions.Add(permission);
                }
            }
            await UserManager.UpdateAsync(user);

            return RedirectToAction("Index", "Default");
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    var allPermissions = await db.Permissions.ToListAsync(); // returns List<Permission>
    var checkBoxListItems = new List<CheckBoxListItem>();
    foreach (var permission in allPermissions)
    {
        checkBoxListItems.Add(new CheckBoxListItem()
        {
            ID = permission.ID,
            Display = permission.Name,
            IsChecked = selectedPermissions.Contains(permission.ID)
        });
    }
    model.Permissions = checkBoxListItems;
    PopulateRolesDropDownList(model.Role);
    return View(model);

}

So on posting the form to create a new user, the user is saved to the database and the selected role is also saved. But when it gets to the line user.Permissions.Add(permission); it fails with the error:

System.NullReferenceException: Object reference not set to an instance of an object.

I've added breakpoints and can see that neither user or permission is null when it gets to the failing line.

It would be much appreciated is someone can any tell me why I'm getting this error?

Richard McKenna
  • 213
  • 4
  • 25

1 Answers1

1

Sounds like user.Permissions is null.

Edit: Rephrased as answer, not question. :-)

boxjar
  • 69
  • 3