0

I have some strange error.

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://11.111.111.11", "user", "1");

DirectorySearcher searcher = new DirectorySearcher(directoryEntry)
{
    PageSize = int.MaxValue,
    Filter = "(&(objectClass=user)(sAMAccountName=" + txt + "*))"
};

SearchResultCollection resultCollection = searcher.FindAll();
foreach (SearchResult result in resultCollection)
{
    if (result != null)
    {
        users.Add(new ObjectTest(
        result.GetDirectoryEntry().Properties["samaccountname"].Value as string,
        result.GetDirectoryEntry().Properties["givenName"].Value as string,
        result.GetDirectoryEntry().Properties["sn"].Value as string,
        result.GetDirectoryEntry().Properties["mail"].Value as string));
    }
}

directoryEntry.Close();

When I search some users I get collection of them and after that use them in foreach. For example: I type user and get collection with 5 users, after that I fill my array.

But some times I get collection with users that give me error here:

For example I type test and I know that I have user "test" and I see my collection with correct count, but after result.GetDirectoryEntry() I get exception Object reference not set to an instance of an object.

I can't find nothing similar on site.

Note: Collection has normal count of objects, and SearchResult is not null it has normal Path! Thank you!

user1711993
  • 271
  • 3
  • 17
  • 1
    [What is NullReference and how do I fix it](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Andy Korneyev Jan 13 '15 at 14:05
  • Note: Collection has normal count of objects, and SearchResult is not null it has normal Path! – user1711993 Jan 13 '15 at 14:06
  • 1
    Could you check if the "result.GetDirectoryEntry()" is not null too? Same question about "Properties[]". – Yury Schkatula Jan 13 '15 at 14:09
  • 1
    So do I understand correctly that it's the `.Value` that's throwing the exception? – adv12 Jan 13 '15 at 14:10
  • YES! result is not null. I get error if I write result.GetDirectoryEntry() – user1711993 Jan 13 '15 at 14:10
  • Not value: this string gives me error result.GetDirectoryEntry() – user1711993 Jan 13 '15 at 14:10
  • @user1711993, that doesn't make sense. If you've tested `result` already (in your `if` test), `result` cannot be null. It has to be something farther along in the code. – adv12 Jan 13 '15 at 14:11
  • 1
    I guess *maybe* `GetDirectoryEntry` itself is throwing a `NullReferenceException` from its own code, but that seems highly unlikely. – adv12 Jan 13 '15 at 14:12
  • adv12 what can you advice me? It's very strange, I get this error on some users, not all – user1711993 Jan 13 '15 at 14:13
  • @user1711993, my best guess is that, for some users, one or more of the `Properties` doesn't exist or is null. You need to break your code into smaller pieces and test the output of each method call/property accessor for null. – adv12 Jan 13 '15 at 14:17
  • @adv12 please write last comment in answers. I will mark as correct. The problem that some fields doesn't typed. Thank you. – user1711993 Jan 13 '15 at 14:28

1 Answers1

1

Here is my comment, answer-ified:

Check the results of the Properties getters; presumably one of them is returning null, and that's where your NullReferenceException is coming from (when you try to get a null property's Value).

Edit: Couldn't delete the answer, but according to @baldpate in the comments, PropertyCollection.Item[string] will never return null, so the above is not the explanation for the NullReferenceException. In any case, the strategy for finding the source of the exception is:

  1. Put each method call on its own line and store the result in a variable.
  2. Step through your code a line at a time, testing outputs until you find the one that's null.
adv12
  • 8,051
  • 2
  • 22
  • 43