Search code examples
entity-framework-coreasp.net-core-mvcasp.net-identity

NullReferenceException when trying to set value to an object


I am trying to add the current userId to my object but I get a nullreferenceexception. I am using a c# .net core MVC project with (Identity authentication).

I think that when I want to create a new Device-Object, the userId is not initated yet but how do I fix that ?

error I get

Controller:

// POST: Device/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Create(Device device)
    {
        if (ModelState.IsValid)
        {
            //get current userId
            string userId = _userManager.GetUserId(HttpContext.User);
            
            //create a device
            int id = _deviceService.Create(device);

            //set userId to the device
            Device deviceToLinkToUser = _deviceService.FindById(id);
            deviceToLinkToUser.User.Id = userId;

            return RedirectToAction(nameof(Details), new { id = id });
        }
        return View(device);
    }

Service:

public int Create(Device device)
    {
            _context.Devices.Add(device);
            _context.SaveChanges();

            return device.Id;
    }

Data-Class:

public class Device
{
    public int Id { get; set; }
    public string Brand { get; set; }
    public string Model { get; set; }

    public Damage Damage { get; set; }
    [Display(Name = "Damage")]
    public int DamageId { get; set; }

    public CustomUser User { get; set; }
}

View:

@model RepairAtDems.Data.Device


@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>
<h4>Device</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Brand" class="control-label"></label>
                <input asp-for="Brand" class="form-control" />
                <span asp-validation-for="Brand" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Model" class="control-label"></label>
                <input asp-for="Model" class="form-control" />
                <span asp-validation-for="Model" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DamageId" class="control-label"></label>
                <select asp-for="DamageId" class="form-control" asp-items="ViewBag.DamageId"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>
<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Am I missing something ?


Solution

  • Your line:

    Device deviceToLinkToUser = _deviceService.FindById(id);
    

    is retrieving a Device, but is probably not 'Include'...ing the User, so when you get to line:

    deviceToLinkToUser.User.Id = userId;
    // deviceToLinkToUser.User == null !!!!!!
    

    so when you try to assign UserId to property 'Id' the owning object 'User' was not found. Hence your error.

    If you want to retrieve a User from the database when you get the Device then you'll need to implement that in Service.FindById() method.

    public Device FindById(int id)
    {
        return _Context.Devices.
            .Include(d => d.User)
            .SingleOrDefault(d => d.Id == id)
    }
    

    If you don't want to retrieve a user and just set the user on the Device then you'll need to use:

    deviceToLinkToUser.User = new CustomUser { Id = userId };