title: C# Object reference not set to an instance of an object. Instantiating Class within a List?
body: <pre><code>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)
      {
  
      }
  }
  </code></pre>
  
  <p>Above is the class, just for some background.</p>
  
  <pre><code>public void AddProduct(string name, decimal price, int quantity)
  {
      lstOrderitem.Add(new OrderItem(name, price, quantity));           
  }
  </code></pre>
  
  <p>On the code inside the AddProduct method is where I am getting the error stated in the title. </p>
  
  <p>I'm just trying to instantiate the class and add it to a collection to be displayed in a listbox on my form program. </p>
  
  <p>The "AddProduct" will be called on a button click event </p>
  
  <blockquote>
    <p>Error = NullReferenceException - Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Edit</p>
  
  <pre><code>    public List&lt;OrderItem&gt; lstOrderitem{ get; private set; }
      public int NumberOfProducts { get; private set; }
      public decimal BasketTotal { get; private set; }
  
      public ShoppingBasket()
      {
          //List&lt;OrderItem&gt; lstOrderitem = new List&lt;OrderItem&gt;();
      }
  
      public void AddProduct(string name, decimal price, int quantity)
      {
          lstOrderitem.Add(new OrderItem(name, price, quantity));
  
  
      }
  </code></pre>