我目前正在为一个涉及构建“电子宠物”程序的 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
即使这些行未注释,代码也会跳过其余的初始化(我已经在方法中注释掉了许多行);这可能与问题有关吗?