DirectoryEntry testAD = new DirectoryEntry();
DirectorySearcher search = new DirectorySearcher(testAD);
StringBuilder add = new StringBuilder();
search.PropertiesToLoad.Add("mail");
search.Filter = "(&(objectClass=user))";
foreach (SearchResult SearchAll in search.FindAll())
{
DirectoryEntry de = SearchAll.GetDirectoryEntry();
add.Append(de.Properties["mail"].Value.ToString()); // error message here
}
PrefixDescription.Text = add.ToString();
I’m trying to find all emails first as a test and then all information (first name, last name, etc) and list it in a text box using a LPAR filter but I keep getting this error message when I run the app:
Object reference not set to an instance of an object.
Well, you’re enumerating users – but you have no guarantee that the resulting user will have an e-mail address! You need basic “programming 101” error prevention:
With this extra check, you avoid the
Object reference not set....error…