【问题标题】:Object not set to instance error with class invocation类调用时对象未设置为实例错误
【发布时间】:2013-05-21 19:47:14
【问题描述】:

我设置了三个模型。我不确定我是否以最好的方式做到了,但这是我可以轻松理解的方式。我调用DepartmentProfile 类。找到的第一个用户不是经理,因此它进入 else 语句并成功填写 AD_UserProfile 并将其添加到 DepartmentProfile 类中。

第二个用户是部门经理,所以它进入 if 语句并在第一行出错,对象未设置为对象实例。我错过了什么?我没有正确设置模型吗?

当它出错时

public class AD_UserProfile
    {
        public string distinguishedName { get; set; }
        public string email { get; set; }
        public string manager { get; set; }
        public string name { get; set; }
        public string userPrincipalName { get; set; } // Useful if we need to allow a user to 'log on' as a different user.  
        public string userName { get; set; } 
    }

public class AD_ManagerProfile
    {
        public string distinguishedName { get; set; }
        public string email { get; set; }
        public string manager { get; set; }
        public string name { get; set; }
        public string userPrincipalName { get; set; } // Useful if we need to allow a user to 'log on' as a different user.  
        public string userName { get; set; }
    }

public class AD_DepartmentProfile
    {
        public AD_DepartmentProfile()
        {
            this.AD_UserProfile = new HashSet<AD_UserProfile>();
        }

        public string name { get; set; }

        public virtual AD_ManagerProfile AD_ManagerProfile { get; set; }
        public virtual ICollection<AD_UserProfile> AD_UserProfile { get; set; }
    }

这是类的调用:

public void GetDepartmentInfo(string department, string owner = "jeremy")
        {
            DirectoryEntry de = new DirectoryEntry("LDAP://server.server.com");

            DirectorySearcher ds = new DirectorySearcher(de);

            ds.Filter = ("(&(objectCategory=person)(objectClass=User)(department=" + department + "))");
            ds.SearchScope = SearchScope.Subtree;

            AD_DepartmentProfile dp = new AD_DepartmentProfile();

            dp.name = department; // assign department name

            foreach (SearchResult temp in ds.FindAll())
            {
                if (owner == temp.Properties["sAMAccountName"][0].ToString())
                {
                    //Current user is manager of department

                    dp.AD_ManagerProfile.distinguishedName = temp.Properties["distinguishedName"][0].ToString();  // This line errors out with instance not set to object error.
                    dp.AD_ManagerProfile.email = temp.Properties["mail"][0].ToString();
                    dp.AD_ManagerProfile.manager = temp.Properties["manager"][0].ToString();
                    dp.AD_ManagerProfile.name = temp.Properties["name"][0].ToString();
                    dp.AD_ManagerProfile.userPrincipalName = temp.Properties["userPrincipalName"][0].ToString();
                    dp.AD_ManagerProfile.userName = temp.Properties["sAMAccountName"][0].ToString();
                }
                else
                {
                    //Current user is in department and does not manage it

                    AD_UserProfile p = new AD_UserProfile();

                    p.distinguishedName = temp.Properties["distinguishedName"][0].ToString();
                    p.email = temp.Properties["mail"][0].ToString();
                    p.manager = temp.Properties["manager"][0].ToString();
                    p.name = temp.Properties["name"][0].ToString();
                    p.userPrincipalName = temp.Properties["userPrincipalName"][0].ToString();
                    p.userName = temp.Properties["sAMAccountName"][0].ToString();

                    dp.AD_UserProfile.Add(p);
                }
            }
        }

【问题讨论】:

标签: c# asp.net-mvc nullreferenceexception


【解决方案1】:

我没有看到您正在初始化 dp.AD_ManagerProfile 的任何地方,所以很可能是 null。您可以在 GetDepartmentInfo 或构造函数中为其赋值。

if (owner == temp.Properties["sAMAccountName"][0].ToString())
{
    //Current user is manager of department

    dp.AD_ManagerProfile = new AD_ManagerProfile();

    dp.AD_ManagerProfile.distinguishedName = temp.Properties["distinguishedName"][0].ToString();  // This line errors out with instance not set to object error.

public AD_DepartmentProfile()
{
    this.AD_UserProfile = new HashSet<AD_UserProfile>();
    this.AD_ManagerProfile = new AD_ManagerProfile();
}

【讨论】:

  • 那一定是我的问题,如何正确初始化呢?它是一个与 AD_UserProfile 完全相同的类,只是我只有一个经理和许多用户。
  • 你打败了我,谢谢你,我会尝试并报告。
  • 那是我的问题,谢谢。我还是 c# 的新手,所以感谢您解释它并展示示例。 :)
  • 既然你是新手,我还要指出,我认为没有必要将 AD_DepartmentProfileAD_UserProfile 设置为单独的类,因为它们具有完全相同的字段并且没有区别行为.在最坏的情况下,可能会有一个包含字段的基类,然后是两个可能具有不同行为的子类。
  • 是的,我也有同样的想法,但我不确定如何将它们结合起来。在我的主课上,我会先打电话给public virtual AD_UserProfile manager_profile { get; set; },然后再打电话给public virtual ICollection&lt;AD_UserProfile&gt; user_profile { get; set }吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 2012-08-31
  • 2011-11-21
  • 2012-10-01
相关资源
最近更新 更多