首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取NullReferenceException

获取NullReferenceException
EN

Stack Overflow用户
提问于 2011-03-16 04:51:30
回答 3查看 1.2K关注 0票数 1

我有一个很有趣的问题。

我有以下架构:

例如,Manager类的实现方式如下:

代码语言:javascript
复制
public sealed class Manager : Interface.Abstract.Employee
{
    private Interface.IEmployee chief = null;
    private readonly Decimal bonuslimit = Convert.ToDecimal(0.4F * Convert.ToSingle(BaseSalary));

    public Manager(Person person, DateTime hiredate) 
    : base(person, hiredate)
    {
    }

    public override List<Interface.IEmployee> Subordinates
    {
        get;
        set;
    }
    public override Interface.IEmployee Chief
    {
        get
        {
            return this.chief;
        }
        set
        {
            //if(value is Associate)
            //{                    
            //    throw new SystemException("Associate can't be a chief");
            //}
            this.chief = value;
        }
    }
    public override Decimal Salary
    {
        get
        {
            var actualbonus = Convert.ToDecimal(0.01F * Convert.ToSingle(this.YearsSinceHired * BaseSalary));
            var bonus = (actualbonus > bonuslimit) ? bonuslimit : actualbonus;
            var additional = 0M;

            if(this.HasSubordinates)
            {
                foreach(Interface.Abstract.Employee employee in this.Subordinates)
                {
                    if(employee is Sales)
                    {
                        additional += employee.Salary;
                    }
                } 
            }
            return Convert.ToDecimal(Convert.ToSingle(additional) * 0.005F) + BaseSalary + bonus;
        }
    }
}

和“工厂客户端”,看起来像这样:

代码语言:javascript
复制
public class EmployeeFactoryClient
{
    private IDictionary<String, IEmployee> employees = new Dictionary<String, IEmployee>();              

    public EmployeeFactoryClient()
    { 
        this.Factory = new EmployeeFactory();            
    }
    public EmployeeFactoryClient(IEmployeeFactory factory)
    {
        this.Factory = factory;            
    }
    public IEmployeeFactory Factory { get; set; }

    public void HireEmployee(Person person, String type, String code)
    {
        this.employees.Add(
            new KeyValuePair<String, IEmployee>(
                code,
                this.Factory.Create(person, type, DateTime.Now)
            )
        );
    }
    public void DismissEmployee(String code)
    {
        this.employees.Remove(code);
    }
    public IEmployee GetEmployee(String code)
    {
        return this.employees[code];
    }
    public IEmployee this[String index]
    {
        get { return this.employees[index]; }
        private set { this.employees[index] = value; }
    }

    public Decimal TotalSalary
    {
        get
        {
            var result = 0M;
            foreach(var item in this.employees)
            {
                result += item.Value.Salary;
            }
            return result;
        }
    }        
}

最后,我有一些测试代码:

代码语言:javascript
复制
public void SalaryTest()
    {
        #region [Persons]            
        var SalesPerson01 = new Person
        {
            Birthday = new DateTime(1980, 11, 03),
            Forename = "Corey",
            Surname = "Black",
            Gender = SexType.Female
        };
        var SalesPerson02 = new Person
        {
            Birthday = new DateTime(1980, 11, 03),
            Forename = "John",
            Surname = "Travis",
            Gender = SexType.Male
        }; 
        #endregion

        this.company.HireEmployee(SalesPerson01, "Sales", SalesPerson01.GetHashCode().ToString());
        ((Employee)this.company[SalesPerson01.GetHashCode().ToString()]).YearsSinceHired = 10;

        this.company.HireEmployee(SalesPerson02, "Sales", SalesPerson02.GetHashCode().ToString());
        ((Employee)this.company[SalesPerson02.GetHashCode().ToString()]).YearsSinceHired = 3;            

        ///////////////////////////////////////////////////////////////////
        ((Employee)this.company[SalesPerson01.GetHashCode().ToString()]).Subordinates.Add(
            this.company[SalesPerson02.GetHashCode().ToString()]
        );

        Assert.AreEqual(1405M, this.company.TotalSalary);
    }

Line ((Employee)this.company[SalesPerson01.GetHashCode().ToString()]).Subordinates.Add(this.company[SalesPerson02.GetHashCode().ToString()]);抛出NullReferenceExeption。在this.company[SalesPerson02.GetHashCode().ToString()]中,索引器返回IEmployee接口,而不是类实例。我说的对吗?如果是这样,我该如何修复它呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-16 04:56:10

我没有看到初始化从属成员的任何地方,所以我怀疑它仍然具有默认值null (不是空列表)。修复方法是在构造函数中将其初始化为一个空列表:

代码语言:javascript
复制
public Manager(Person person, DateTime hiredate) : base(person, hiredate)
{
    Subordinates = new List<Interface.IEmployee>();
}
票数 3
EN

Stack Overflow用户

发布于 2011-03-16 05:04:06

看起来你把Person放到了Person中,然后将其转换为Employee,这是不相关的类

票数 0
EN

Stack Overflow用户

发布于 2011-03-16 05:08:29

索引器返回IEmployee接口,但不返回类实例。我说的对吗?

你没有正确的索引器必须返回实例,从接口创建实例是不可能的。

我认为this.companySalesPerson02.GetHashCode().ToString()会返回null,因为您没有将SalesPerson02实例添加到公司对象中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5317895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档