2

I have a simple details view that uses two if statements if the user is a Customer, it displays the customer properties. If the user is an Investor it displays their properties instead.

My issue is that my if statement works for one or the other but not both. Giving me a:

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

when trying to use both if statements.


My view

@model MyProject.Models.ApplicationUser
<h3>
    @Html.DisplayFor(m => Model.FirstName)
    @Html.DisplayFor(m => Model.LastName)
</h3>
@if (Model.Customer.CustomerId != null)
{
    <div class="form-group">
        <strong>Tax ID:</strong>
        @Html.DisplayFor(m => m.Customer.TaxId)
    </div>
} else {
}

@if (Model.Investor.InvestorId != null)
{
    <div class="form-group">
        <strong>Social Security #:</strong>
        @Html.DisplayFor(m => m.Investor.SsnNum)
    </div>
    <div class="form-group"> 
        <strong>Date of birth:</strong>
        @Html.DisplayFor(m => m.Investor.DOB)
    </div>
} else {
}

Controller

public async Task<IActionResult> Details(string id)
{
    if (id == null || id.Trim().Length == 0)
    {
        return NotFound();
    }
    var userFromDb = await _db.ApplicationUser.Include(u => u.Investor).Include(u => u.Customer).FirstOrDefaultAsync(i => i.Id == id);
    if (userFromDb == null)
    {
        return NotFound();
    }
    return View(userFromDb);
}

Investor

public class Investor
{
    [Key, ForeignKey("ApplicationUser")]
    public string InvestorId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }

    [Required]
    [Display(Name = "SSN")]
    public string SsnNum { get; set; }
    [Display(Name = "Date of Birth")]
    public DateTime DOB { get; set; }
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
robert_rg
  • 275
  • 1
  • 5
  • 13
  • 2
    Show us how you populate `Customer` and `Investor`. I suspect one of them is not initialized hence you would get NULL reference error. – David Liang Mar 11 '19 at 18:15
  • Instead of `if (Model.Customer.CustomerId != null)` have `if (Model.Customer != null && Model.Customer.CustomerId != null)`. – Wiktor Zychla Mar 11 '19 at 18:20
  • The view works with the if statement until I use both if statements then it throws an error. I can use the if statement for investor and it works or customer and it works, just not both. – robert_rg Mar 11 '19 at 18:23
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Igor Mar 11 '19 at 18:31
  • 1
    Because one or the other with *always* be null. It's either going to have a `Customer` or it's going to have an `Investor`, but never both. You should write your conditionals like `Model.Customer != null && Model.Customer.CustomerId != null` like @WiktorZychla suggested. You can also use the null conditional operator like `Model.Customer?.CustomerId != null`, which is a little easier. – Chris Pratt Mar 11 '19 at 19:25
  • Thanks, I was just thinking that before I saw your comment and the code you provided fixed the issue – robert_rg Mar 13 '19 at 17:30
  • @ChrisPratt why not `Model.Customer?.CustomerId != null` :) – Erik Philips Mar 13 '19 at 17:37
  • @ErikPhilips: Looks like what I had already to my eyes. Am I missing something? – Chris Pratt Mar 13 '19 at 18:38
  • @ChrisPratt yes.. you're missing the fact that I didn't read lol – Erik Philips Mar 13 '19 at 23:29

1 Answers1

1

Actually, you are trying to access a property of Customer or Investor which is not initialized. First of all check, that weather Customer or Investor is null or not then check for its properties.

@model MyProject.Models.ApplicationUser
<h3>
    @Html.DisplayFor(m => Model.FirstName)
    @Html.DisplayFor(m => Model.LastName)
</h3>
@if (Model.Customer?.CustomerId != null)
{
    <div class="form-group">
        <strong>Tax ID:</strong>
        @Html.DisplayFor(m => m.Customer.TaxId)
    </div>
} else {
}

@if (Model.Investor?.InvestorId != null)
{
    <div class="form-group">
        <strong>Social Security #:</strong>
        @Html.DisplayFor(m => m.Investor.SsnNum)
    </div>
    <div class="form-group"> 
        <strong>Date of birth:</strong>
        @Html.DisplayFor(m => m.Investor.DOB)
    </div>
} else {
}
habib
  • 2,366
  • 5
  • 25
  • 41