0

I set up three models. I'm not sure if I did it the best way, but it's the way I could understand it easily. I invoke the DepartmentProfile class. The first user found is not a manager so it goes into the else statement and successfully fills in the AD_UserProfile and adds it to the DepartmentProfile class.

The second user is the department manager so it goes into the if statement and errors out on the very first line with object not set to instance of object. What am I missing? Did I not set up the models correctly?

When it errors out

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

Here is the invocation of the classes:

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);
                }
            }
        }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
James Wilson
  • 5,074
  • 16
  • 63
  • 122
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 21 '13 at 20:05

1 Answers1

2

I don't see anywhere that you're initializing dp.AD_ManagerProfile, so it is likely null. You could either give it a value in GetDepartmentInfo or in the constructor.

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.

or

public AD_DepartmentProfile()
{
    this.AD_UserProfile = new HashSet<AD_UserProfile>();
    this.AD_ManagerProfile = new AD_ManagerProfile();
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • That must be my problem, how do I properly initialize it? It is a class that is exactly the same as AD_UserProfile except I only have one manager and many users. – James Wilson May 21 '13 at 19:51
  • You beat me to it, thank you I will try that and report back. – James Wilson May 21 '13 at 19:52
  • That was my problem, thank you. I'm still new to c# so thanks for explaining it and showing example. :) – James Wilson May 21 '13 at 19:53
  • Since you're new I'll also point out that I don't see a need to have `AD_DepartmentProfile` and `AD_UserProfile` be separate classes since they have the exact same fields and no differentiating behavior. At worst there could be a base class that contains the fields and then two subclasses that _could_ have different behavior. – D Stanley May 21 '13 at 21:39
  • Yeah I thought the same thing, but I wasn't sure how to combine them. On my main class would I just call `public virtual AD_UserProfile manager_profile { get; set; }` and then `public virtual ICollection user_profile { get; set }`? – James Wilson May 21 '13 at 22:16