0

In my model I have this particular property to add two different model values and show it in my index page.

 public decimal Amount
    {
        get { return Products.Price * Qty; }

    }

When I go to my create page and try to create another sale after I hit submit I get a yellow screen of death with the following:

Object reference not set to an instance of an object.

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

It points to this part of my model get { return Products.Price * Qty; }

I'm not really interested in storing anything from here, and I don't even use the Amount property in my create page. Is there a way to tell it to ignore on submit?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Shido
  • 35
  • 2
  • 6
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 11 '14 at 00:25
  • 1
    He's not asking what a null reference exception is. He's asking how to get around it in this scenario. – Bob Horn Dec 11 '14 at 00:26
  • The one question pretty much answers the other. – John Saunders Dec 11 '14 at 00:49

1 Answers1

0

How about this?

public decimal Amount
{        
    get
    {
        if (Products == null) { return 0; }
        return Products.Price * Qty;
    }
}
Bob Horn
  • 33,387
  • 34
  • 113
  • 219