title: Null reference exception c++
body: <p>I keep getting this error in the console: </p>
  
  <blockquote>
    <p>Unhandled exception: System.NullReferenceException</p>
  </blockquote>
  
  <p>Here's the code:</p>
  
  <pre><code>class Car {
  public:
     int X;
     int Y;
  };
  
  class SpecificCar : public Car {
  
  };
  
  class Container {
  public:
     int AmountOfCars = 0;
     Car **cars = nullptr;
     void AddCar(Car *ptr);
  };
  
  void Container::AddCar(Car *ptr) {
      if(AmountOfCars == 0) {
      cars[0] = ptr; //Debbuger says that the problem in question is located here
      AmountOfCars++;
      }
  
  int main() {
      Container container;
      Car *ptr = new SpecificCar;
      ptr-&gt;X = 1;
      ptr-&gt;Y = 5;
      container.AddCar(ptr);
  }
  </code></pre>