나는 'tamagotchi'프로그램을 만드는 것을 포함하는 uni 프로젝트를 위해 현재 일하고있다. 그러나 나는 각기 관련된 값을 저장하기 위해 사용한 클래스 구조와 관련하여 아주 일찍 오류를 범했다. 애완 동물은 기록의 일부로 그러나 프로그램을 추적하면 변수가 초기화 된 것처럼 보이지 않고 프로그램이 변수를 처음 호출하면 NullReferenceException
까지 발생합니다. 왜 어떤 아이디어?이 정적 클래스가 초기화되지 않는 이유는 무엇입니까?
static class GlobalVars // Static class used to store pet values as 'global' variables.
{
public static TTamagotchiPet Pet1 { get; set; }
public static TTamagotchiPet Pet2 { get; set; }
public static TTamagotchiPet Pet3 { get; set; }
public static TTamagotchiPet Pet4 { get; set; }
}
public void frmTamagotchi_Load(object sender, EventArgs e) // On Load event; initialises Pet 1.
{
tmrUpdate.Enabled = true;
GlobalVars.Pet1.Active = true;
//GlobalVars.Pet1.Dead = false;
//GlobalVars.Pet1.FunValue = 0;
//GlobalVars.Pet1.FoodValue = 0;
//GlobalVars.Pet1.HealthValue = 0;
//GlobalVars.Pet1.ExertionValue = 0;
//GlobalVars.Pet2.Active = false;
//GlobalVars.Pet3.Active = false;
//GlobalVars.Pet4.Active = false;
}
private void tmrUpdate_Tick(object sender, EventArgs e) // Update timer. Each tick reduces pet attributes and checks to see if a pet has died, and controls pet states for the animation timer.
{
// First section updates pet attributes and checks to see if health reaches the 100 limit - at which point the pet dies.
if (GlobalVars.Pet1.Active == true) //code crashes here
{
if (GlobalVars.Pet1.Dead == false)
{
는 코드는 선이 주석에도 (필자는 frmTamagotchi_load
방법에 많은 라인을 주석 한) 초기화의 나머지 부분을 건너 뛰고; 이 문제와 관련이있을 수 있습니까?
당신은 초기화 된 적이없는 정적 속성의 무리가 있습니다
당신이이를 원하시면
는 선언의 시점에서 과제를 수행해야default(T)
이외의 값으로 초기화한다 모두 'null'. – Oded[.NET의 NullReferenceException은 무엇입니까?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) "간접적 인"사례를 참조하십시오. – dtb