2014-05-12 72 views
0

这里是问题: 我试图在类的窗帘方法中触发事件。 例如,我得到了一个名为clMage的类,它从clUnits继承,并且获得了公共方法attMagicMissle(clUnit aU)。 那么这里是为clUnits声明代码:NullReferenceExeption在事件触发时未处理

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; 
    } 
} 

这里进入主():

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,但我只是无法得到为什么在评论线为CL法师它给了我这个错误,因此无法理解如何解决它!

+2

几乎'NullReferenceException'的所有情况都是一样的。请参阅“[什么是.NET一个NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)”获得一些提示。 –

+0

建议:你不必用'cl'将所有类加上前缀。没有什么特别的类,他们需要一个特殊的前缀。它只是让你的代码更不易读。 –

+0

Thx链接阅读... –

回答

1

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

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

通常情况下,人们创造这样的方法来引发事件:

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

在一些我已经指出了事件侦听事件的重要性 - 我用来了解事件的来源没有指出,如果“侦听器”不存在,则处理程序将返回null 。除此之外,我认为必须指出的另一件事是:必须在触发事件的行之前声明侦听器,否则处理程序将返回null。希望我提出的这个话题能够对其他迷失在“事件迷宫”中的人有所帮助。并且非常感谢Thomas Levesque为我解决问题提供了领先点。 –

相关问题