Array.Find -- Object reference not set to an instance of an object

I am trying to find filenames in a FileInfo Array
           WORKING_FILENAME = strDocFileName
            Dim DocFileFound As FileInfo
            DocFileFound = Array.Find(allArchiveDocs, AddressOf ContainsFileName)

            Try
                If DocFileFound.Length > 0 Then
                    dsCIAndArchiveData.Tables(0).Rows(i).Item("FullPath") = DocFileFound.FullName
                Else
                    dsCIAndArchiveData.Tables(0).Rows(i).Item("FullPath") = "NO FILE FOUND"
                End If
            Catch ex As Exception
                MessageBox.Show("Error finding File: " & ex.Message)
            End Try

Protected Function ContainsFileName(ByVal AllFileNames As FileInfo) As Boolean

        If AllFileNames.Name = WORKING_FILENAME Then
            Return True
        Else
            Return False
        End If

    End Function

Open in new window


This works, unless ContainsFileName cannot find the file which results in an "Object reference not set...(error in title)".  How can I check the DocFileFound before accessing?
frontback45Asked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try changing:

    If DocFileFound.Length > 0 Then

To:

    If Not IsNothing(DocFileFound) Then
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.