12
public class OrderItem
{
    public string ProductName { get; private set; }
    public decimal LatestPrice { get; private set; }
    public int Quantity { get; private set; }
    public decimal TotalOrder { get {return LatestPrice * Quantity;}}

    public OrderItem(string name, decimal price, int quantity)
    {

    }

    public OrderItem(string name, decimal price) : this(name, price, 1)
    {

    }
}

Above is the class, just for some background.

public void AddProduct(string name, decimal price, int quantity)
{
    lstOrderitem.Add(new OrderItem(name, price, quantity));           
}

On the code inside the AddProduct method is where I am getting the error stated in the title.

I'm just trying to instantiate the class and add it to a collection to be displayed in a listbox on my form program.

The "AddProduct" will be called on a button click event

Error = NullReferenceException - Object reference not set to an instance of an object.

I was wondering if anybody knew why this was happening since I thought that since I am making a NEW instance of the class while adding it to the list that it would have something to reference too. Thank you if anybody knows what the problem is.

Edit

    public List<OrderItem> lstOrderitem{ get; private set; }
    public int NumberOfProducts { get; private set; }
    public decimal BasketTotal { get; private set; }

    public ShoppingBasket()
    {
        //List<OrderItem> lstOrderitem = new List<OrderItem>();
    }

    public void AddProduct(string name, decimal price, int quantity)
    {
        lstOrderitem.Add(new OrderItem(name, price, quantity));


    }
Taemint
  • 141
  • 1
  • 1
  • 6

3 Answers3

33

You should initialize lstOrderitem property in the constructor, like this:

EDIT

public MyClass() {
    lstOrderitem = new List<OrderItem>();
}

P.S. Microsoft suggests starting the names of your properties in capital letters, to avoid confusion with member variables, which should be named starting with a lowercase letter.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • The "lstOrderItem is a property sorry I should have included that part. Edited the OP to show this – Taemint Jan 02 '12 at 14:22
  • @Taelmit: even if this is a `auto` property you have to initialize it, like an answer provided and you did. – Tigran Jan 02 '12 at 14:24
  • Why the compiler doesn't complain when we try to use an instance property that has not been initialized? – JayJay Feb 01 '17 at 15:39
  • @JayJay Because compiler implicitly initializes instance properties, making sure that there is no such thing as an instance property that has not been initialized. – Sergey Kalinichenko Feb 01 '17 at 15:42
4

It looks like you didn't initialize your reference lstOrderitem. Debug your code if your references value is null, you need to initialize lstOrderitem before using it.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
Thomas
  • 232
  • 1
  • 2
  • 8
  • The "lstOrderItem is a property sorry I should have included that part. Edited the OP to show this – Taemint Jan 02 '12 at 14:21
0

It looks like you didn't initialize your reference lstOrderitem. Debug your code if your reference value is null, you need to initialize lstOrderitem before using it.

public MyClass() {
    lstOrderitem = new List<OrderItem>();
}
vvns
  • 3,548
  • 3
  • 41
  • 57
mhine
  • 1