对象未设置为类调用时的实例错误
本文关键字:实例 错误 调用 设置 对象 | 更新日期: 2023-09-27 18:14:36
我设置了三个模型。我不确定我是不是用了最好的方法,但这是我能很容易理解的方法。我调用DepartmentProfile
类。找到的第一个用户不是管理器,因此它进入else语句,成功地填充了AD_UserProfile
并将其添加到DepartmentProfile
类。
第二个用户是部门经理,因此它进入if语句并在第一行出错,因为object没有设置为object的实例。我错过了什么?我没有正确设置模型吗?
出错时
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);
}
}
}
我没有看到任何地方初始化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();
}