事件触发时未处理NullReferenceException

本文关键字:未处理 NullReferenceException 事件 | 更新日期: 2023-09-27 18:03:14

问题是:我试图在一个类的窗帘方法中触发一个事件。例如,我有一个名为clMage的类继承自clUnits,它有公共方法attmagicmissile (clUnit aU)。下面是clUnits声明的代码:

    public class clUnits
    {
        public int iHitPoints { get; set; }
        public int iDamage { get; set; }
        public ArmorType unitArmor { get; set; }
    }

这里是一个带有麻烦方法的clMage:

    public class clMage : clUnits
    {
        public event evtDamageDone damageDealtToSoldier;
        public event evtDamageDone damageDealtToArcher;
        public event evtDamageDone damageDealtToMage;
        public clUnits currentTarget { get; set; }
        public AttackType mageAttack { get; set; }
        public clMage(int iHP, int iDamage, AttackType atType, ArmorType arType)
        {
            this.iHitPoints = iHP;
            this.iDamage = iDamage;
            this.mageAttack = atType;
            this.unitArmor = arType;
        }
        public int attMagicMissle(clUnits aU)
        {
            int iDamageDeals = 0;
            currentTarget = aU;
            switch (currentTarget.unitArmor)
            {
                case ArmorType.None:
                    {
                        iDamageDeals = iDamage * 2;
                    }

          break;
            case ArmorType.Heavy:
                {
                    iDamageDeals = Convert.ToInt32(iDamage * 1.5);
                    this.damageDealtToSoldier(currentTarget); // Here is the NullReferenceExeption problem starts
                }
                break;
            case ArmorType.Medium:
                {
                    iDamageDeals = Convert.ToInt32(iDamage * 0.5);
                    this.damageDealtToArcher(currentTarget);
                } break;
            case ArmorType.Light: 
                { 
                    iDamageDeals = iDamage;
                    this.damageDealtToMage(currentTarget);
                } break;
        }
        return iDamageDeals;
    }
}

Main():

    public delegate int Attack(clUnits aUnit);
    public delegate void evtDamageDone(object aUnit);
    public enum AttackType { None, Melee, Range, Mage }
    public enum ArmorType { None, Heavy, Medium, Light}
    class Program
    {
        static void Main(string[] args)
        {
            Attack Strikes;
            clWarrior theSoldier = new clWarrior(750, 75, AttackType.Melee, ArmorType.Heavy);
            clArcher theArcher = new clArcher(500, 100, AttackType.Range, ArmorType.Medium);
            clMage theMage = new clMage(250, 150, AttackType.Mage, ArmorType.Light);
            // Mage actions
            Console.WriteLine("The mage: ");
            Strikes = theMage.attMagicMissle;
            Console.WriteLine("Attack hase damage = {0} attacks the soldier for {1} damage!", theMage.iDamage, Strikes(theSoldier));
            Console.WriteLine("Attack hase damage = {0} attacks the archer for {1} damage!", theMage.iDamage, Strikes(theArcher));
            Console.WriteLine("Attack hase damage = {0} attacks the mage for {1} damage!", theMage.iDamage, Strikes(theMage));
            // Archer actions
            Console.WriteLine("The archer: ");
            Strikes = theArcher.attArrowShot;
            Console.WriteLine("Attack hase damage = {0} attacks the soldier for {1} damage!", theArcher.iDamage, Strikes(theSoldier));
            Console.WriteLine("Attack hase damage = {0} attacks the archer for {1} damage!", theArcher.iDamage, Strikes(theArcher));
            Console.WriteLine("Attack hase damage = {0} attacks the mage for {1} damage!", theArcher.iDamage, Strikes(theMage));
            // Soldier actions
            Console.WriteLine("The soldier: ");
            Strikes = theSoldier.attSwordSlash;
            Console.WriteLine("Attack hase damage = {0} attacks the soldier for {1} damage!", theSoldier.iDamage, Strikes(theSoldier));
            Console.WriteLine("Attack hase damage = {0} attacks the archer for {1} damage!", theSoldier.iDamage, Strikes(theArcher));
            Console.WriteLine("Attack hase damage = {0} attacks the mage for {1} damage!", theSoldier.iDamage, Strikes(theMage));
        }
    }

嗯嗯…我已经坐了相当长的一段时间了(9个小时),主要是在网上冲浪和stackoverflow,但我就是不明白为什么在clMage的注释行上它给了我这个错误,因此无法理解如何修复它!

事件触发时未处理NullReferenceException

如果没有人订阅它,则damageDealtToSoldier为空,因此调用它会导致NullReferenceException。您需要检查它是否为null:

if (this.damageDealtToSoldier != null)
    this.damageDealtToSoldier(currentTarget);

通常,人们创建这样一个方法来引发事件:

protected virtual void OnDamageDealtToSoldier(object aUnit)
{
    var handler = this.damageDealtToSoldier;
    if (handler != null)
        handler(aUnit);
}