1

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!

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Skrubb
  • 531
  • 1
  • 6
  • 25
  • You haven't instantiated your object and are trying to use it... – Brian Nov 11 '13 at 17:18
  • Could you clarify that? I'm still a little new to programming. :-) – Skrubb Nov 11 '13 at 17:19
  • [Initializing objects in C#](http://msdn.microsoft.com/en-us/library/vstudio/bb397680.aspx). Basically, what I am saying is that you have some object 'x'. In order to use it (in most cases) you need to 'new' the object up like this: `Object obj = new Object();`. Using `var` in your code, you are _implicitly_ typing your object rather than _explicitly_ typing it. – Brian Nov 11 '13 at 17:51
  • Try this in your code: `var org = new OrganizationRepo.GetByID(dd.OrganizationID);` and see if that fixes your problem. – Brian Nov 11 '13 at 17:55
  • That line is already in there! It's right above the if(ModelStat.IsValid). . . When I mouse over the dd.DomainAddress on the Remove part of the controller, it's showing that as null, I'm sure that is where the error is coming from, I just don't know how to solve it. – Skrubb Nov 11 '13 at 18:02
  • Re-read my comment. I said: `var org = NEW OrganizationRepo.GetByID(dd.OrganizationID);`. Notice the `new` keyword... – Brian Nov 11 '13 at 18:04
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – tom redfern Jan 11 '17 at 10:27

1 Answers1

1

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.

Markus
  • 20,838
  • 4
  • 31
  • 55