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

Posted on 2011-05-02
Last Modified: 2013-11-26
I am trying to find filenames in a FileInfo Array
           WORKING_FILENAME = strDocFileName
            Dim DocFileFound As FileInfo
            DocFileFound = Array.Find(allArchiveDocs, AddressOf ContainsFileName)

                If DocFileFound.Length > 0 Then
                    dsCIAndArchiveData.Tables(0).Rows(i).Item("FullPath") = DocFileFound.FullName
                    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
            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?
Question by:frontback45
    1 Comment
    LVL 85

    Accepted Solution

    Try changing:

        If DocFileFound.Length > 0 Then


        If Not IsNothing(DocFileFound) Then

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    This is an explanation of a simple data model to help parse a JSON feed
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now