0

I have a function that searches AD for an attribute (extensionAttribute3). It works fine if the attribute has a value, but if it's not set, it errors with 'Object reference not set to an instance of an object.' This always errors on the line:

CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString

I've tried using "If IsNothing" and "IsNot Nothing" to check for NULL values on both dirResult & CurrentPIN, but it still errors. How can I check for NULL values successfully?

Checking if dirResult is NULL:

Private Function GetUserProperties()
    Dim ADName As String = GetLogonName()
    Dim CurrentPIN As String = Nothing
    Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
    Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
    dirSearcher.Filter = ("(samAccountName=" & ADName & ")")
    dirSearcher.PropertiesToLoad.Add("extensionAttribute3")
    dirSearcher.SearchScope = SearchScope.Subtree
    Dim dirResult As SearchResult = dirSearcher.FindOne()
    If IsNothing(dirResult) Then
        Return "<not set>"
    Else
        CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
        Return CurrentPIN
    End If
End Function

Checking if CurrentPIN is NULL:

   Private Function GetUserProperties()
        Dim ADName As String = GetLogonName()
        Dim CurrentPIN As String = Nothing
        Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
        dirSearcher.Filter = ("(samAccountName=" & ADName & ")")
        dirSearcher.PropertiesToLoad.Add("extensionAttribute3")
        dirSearcher.SearchScope = SearchScope.Subtree
        Dim dirResult As SearchResult = dirSearcher.FindOne()
        CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
        If IsNothing(CurrentPIN) Then
            Return False
        Else
            Return CurrentPIN
        End If
    End Function
Ben Blackmore
  • 35
  • 2
  • 7
  • Not a duplicate but a very recommended reading [What is a NullReferenceException and how to fix it](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) Check the answer dedicated to VB.NET – Steve Nov 18 '14 at 23:33
  • You just have to place the null check at the right spot. Which is for `Properties("extensionAttribute3)`. Most likely to be nothing, you can't use the Value property of Nothing. – Hans Passant Nov 19 '14 at 00:01

1 Answers1

1

You should check for NULL in Properties("extensionAttribute3"):

If dirResult Is Nothing OrElse dirResult.GetDirectoryEntry.Properties("extensionAttribute3") Is Nothing Then
    Return "<not set>"
Else
    CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
    Return CurrentPIN
End If

Explanation:

Is Nothing is IMO a cleaner way to check for NULLs, and AFAIK it also saves you a stack level.

If extensionAttribute3 doesn't have a value, you are having the exception because

CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString

is translated to

CurrentPIN = <NULL>.Value.ToString

Checking for NULL on dirResult is just to prevent a possible future exception.

Josh Part
  • 1,991
  • 10
  • 13
  • Worked, just had to make 1 small change, I needed to add .Value on the OrElse string, as it was giving the same 'Object reference not set to an instance of an object.' error. `If dirResult Is Nothing OrElse dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value Is Nothing Then` – Ben Blackmore Nov 19 '14 at 09:26