title: Getting Object not set to an instance - Directory Services
body: <p>I'm trying to retrieve the manager email base on a user.</p>
  
  <p>I did investigated about this, and I found this code but getting "Object not set to an instance", I do not know what do I need to initialize.</p>
  
  <p>If someone can give me a hint. I appreciate it.</p>
  
  <pre><code>    public string GetEmail(string userId)
      {
  
          PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, MyDomain);
          UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userId);
          return user.EmailAddress;
      }
  </code></pre>