I have a class Garage that has a property which is an array of type Car, which is another class in the program. I've tried several iterations and get run-time errors on most of them. I get a NullRefernceException whenever I try to run it. This happens in the Program class where I try to access the length property of the CarLot array.
I know this has something to do with the CarLot property of the Garage class being an array instead of just Car. What piece am I missing here so that the array isn't set to null when the program tries to use it?
class Program
{
static void Main(string[] args)
{
Garage g = new Garage();
//this is where the exception occurs
g.CarLot[0] = new Car(5, "car model");
Console.ReadLine();
}
}
public class Garage
{
public Car[] CarLot { get; set; }
public Garage() { }
//this should be able to be 0 or greater
public Garage(params Car[] c)
{
Car[] cars = { };
CarLot = cars;
}
}
public class Car
{
public int VIN { get; set; }
public int Year { get; set; }
public string Model { get; set; }
public Car(int _vin, string _model)
{
_vin = VIN;
_model = Model;
}
public Car() { }
public void Print()
{
Console.WriteLine("Here is some information about the car {0} and {1} ");
}
}
NullReferenceExceptionon the lineg.CarLot..in the Program class