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; }
}
9
  • 2
    Show us how you populate Customer and Investor. I suspect one of them is not initialized hence you would get NULL reference error. Commented Mar 11, 2019 at 18:15
  • Instead of if (Model.Customer.CustomerId != null) have if (Model.Customer != null && Model.Customer.CustomerId != null). Commented Mar 11, 2019 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. Commented Mar 11, 2019 at 18:23
  • 2
    Possible duplicate of What is a NullReferenceException, and how do I fix it? Commented Mar 11, 2019 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. Commented Mar 11, 2019 at 19:25

1 Answer 1

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 {
}
Sign up to request clarification or add additional context in comments.

4 Comments

You logic can be shortened to Model.Custromer?.CustomerId != null and Model.Investor?.InvestorId != null.
@Erik Philips yes, But I didn't know which version of C# he is using. Definitely, If he is using C# 5.0 or above then can use it.
Consider the Details method, it is using async/await only came out in c# 5.0 :)
@ErikPhilips Yeh sure, I didn't consider that. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.