首页 > 解决方案 > 设置变量给我一个 NullReferenceException

问题描述

嘿,我正在做一个学校项目,当我设置一个类的变量时,它会抛出一个 NullReferenceException 你可以找到完整的项目@ https://github.com/kyllianlissens/BugManagment

if (!dataReader.IsDBNull(dataReader.GetOrdinal("user_id")))
{
    task.Employee = (Employee) UserRepository.Items.Find(x => x.Id.Equals(Convert.ToInt32(dataReader["user_id"])));
}

它发生在设置 task.Employee 变量时

这是我的任务课

public class Task : Entity
{
    internal Task(int id, Bug bug, string description, int size, TimeSpan timeSpent) : base(id)
    {
        Bug = bug;
        Size = size;
        TimeSpent = timeSpent;
        Description = description;
        Employee = null;
    }

    public Employee Employee { get; internal set; }
    public Bug Bug { get; internal set; }
    public string Description { get; internal set; }
    public int Size { get; internal set; }
    public TimeSpan TimeSpent { get; internal set; }

    internal void AddTime(TimeSpan time)
    {
        TimeSpent += time;
    }
}

我还尝试删除 Employee = null; 但这并没有改变任何事情。

标签: c#oop

解决方案


推荐阅读