나는 프로그램의 다른 클래스 인 Car
유형의 배열 인 속성을 갖는 Garage
클래스를 가지고 있습니다. 몇 번의 반복을 시도하고 대부분의 런타임 오류가 발생합니다. 나는 그것을 실행할 때마다 NullRefernceException
을 얻습니다. 이것은 CarLot
배열의 길이 속성에 액세스하려고 시도하는 클래스 Program
에서 발생합니다.생성자에서 클래스 배열 초기화
나는 대신에 배열이 클래스 인 CarLot
속성과 관련이 있다는 것을 알고 있습니다. 내가 배열을 null로 설정할 수 있도록 프로그램을 사용하려고 할 때 어떤 조각이 누락 되었습니까?
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} ");
}
}
정확한 예외는 무엇입니까? –
'NullReferenceException'은'g.CarLot ..' 프로그램 클래스에 있습니다 – wootscootinboogie