Skrubb
Skrubb

Reputation: 541

Object reference not set to an instance of an object error on the controller

I am unsure why I'm getting this error:

Object reference not set to an instance of an object.

I have a db that has some organizations in it, each organization can have multiple domains. the way I have it set up is to have just a regular actionLink to delete a domain. Here is what the code looks like:

public ActionResult DeleteDomain(OrganizationDomainAddressView dd)
    {
        var org = OrganizationRepo.GetByID(dd.OrganizationID);
        if (ModelState.IsValid)
        {
            org.DomainAddresses.Remove(new OrganizationDomainAddress(dd.OrganizationID, dd.DomainAddress));
            OrganizationRepo.Delete(org);
        }
        OrganizationView orgInfo = (OrganizationView)Mapper.DynamicMap(org, typeof(Organization), typeof(OrganizationView));
        var domainList = orgInfo.DomainAddresses;
        var ACD = new OrganizationDomainAddressView();
        ViewData["ACD"] = ACD;
        ViewBag.OrganizationID = dd.OrganizationID;
        return PartialView("_EmailDomainPartial", domainList);
    }

Here is the View that i'm working with:

@model IEnumerable<AdminTool.Models.ViewModel.OrganizationDomainAddressView>
@using AdminTool.Models.ViewModel;
@{
OrganizationDomainAddressView ACD = (OrganizationDomainAddressView) ViewData["ACD"];
}

<div id="domains">
@foreach (var item in Model)
{
    <div class="item">
        @item.DomainAddress &mdash; @Html.ActionLink("Delete", "DeleteDomain", "Organizations", new { dd = item.DomainAddress })
    </div>
}
<div class="item">
    @Html.Hidden("OrganizationID", (int)ViewBag.OrganizationID)
</div>
@Html.Partial("_EmailDomainAddPartial", ACD)
</div>

and the viewmodel:

public class OrganizationDomainAddressView
{
    public OrganizationDomainAddressView(string domainAddress, int organizationId)
    {
        DomainAddress = domainAddress;
        OrganizationID = organizationId;
    }

    public OrganizationDomainAddressView()
    { }

    [AtLeastOneRequired(ErrorMessageResourceType = typeof(AdminResource), ErrorMessageResourceName = "OrganizationEmailDomainRequired")]
    [Display(ResourceType = typeof(AdminResource), Name = "OrganizationValidEmailDomain")]
    [RegularExpression(@"@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessageResourceType = typeof(AdminResource), ErrorMessageResourceName = "OrganizationEmailFormatError")]
    public string DomainAddress { get; set; }

    public int OrganizationID { get; set; }
}

The code is erroring out on the controller where the line is:

org.DomainAddresses.Remove(new OrganizationDomainAddress(dd.OrganizationID, dd.DomainAddress));

Not sure why this is. Any help would be great!

Upvotes: 1

Views: 995

Answers (1)

Markus
Markus

Reputation: 22511

Based upon the line that the error is raised at, check in the debugger whether the following parts of code are null:

  • org
  • org.DomainAdresses

If dd was not set, the error would already appear dome lines above. Depending on your implementation, maybe the constructor of OrganizationDomainAddress throws a NullReferenceException if one of the input values is null.

Upvotes: 1

Related Questions