Object reference not set to an instance of an object?

x38class-9313 141 Reputation points
2022-09-04T05:44:23.137+00:00

I have the following code to find a specific file in a directory & all its sub directories

Public Function DoRecursiveSearch(ByVal DirPath As String, _  
Optional ByVal FileSearchPattern As String = "*.*", _  
Optional ByVal FolderSearchPattern As String = "*", _  
Optional ByVal Recursive As Boolean = True) As ArrayList  
  
        ' Specify the directories you want to manipulate.  
        Dim di As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(DirPath)  
        Dim ar As New ArrayList  
        Try  
            ' Get only subdirectories   
            Dim dirs As System.IO.FileSystemInfo() = di.GetDirectories(FolderSearchPattern)  
            Dim files As System.IO.FileSystemInfo()  
            Dim diNext As System.IO.DirectoryInfo  
            Dim fiNext As System.IO.FileInfo  
  
            files = di.GetFiles(FileSearchPattern)  
            For Each fiNext In files  
                ar.Add(fiNext)  
            Next  
  
            If Recursive Then  
                For Each diNext In dirs  
                    ar.AddRange(DoRecursiveSearch(diNext.FullName, FileSearchPattern, FolderSearchPattern, Recursive))  
                Next  
            End If  
  
            Return ar  
        Catch e As Exception  
            Console.WriteLine("The process failed: {0}", e.ToString())  
        End Try  
  
    End Function  
  
Public Function GetDirectoryLocation(ByVal SearchDir As String, ByVal searchFile As String) As String  
        '"C:\Program Files\Norton Security\"  
  
  
        Dim msg As String  
        Dim ar As New ArrayList  
        Dim fi As System.IO.FileInfo  
        'ar = GetFiles(Server.MapPath("."), "*.aspx")  
        ar = DoRecursiveSearch(SearchDir, searchFile)  
        'Response.Write("<Pre>")  
  
        For Each fi In ar  
            NortonLocation = fi.FullName  
            'msg = msg & "File Path: " & fi.FullName & vbCrLf  
            'msg = msg & "File Size: " & fi.Length & vbCrLf  
            'msg = msg & "Last Modified: " & fi.LastWriteTime & vbCrLf  
            'msg = msg & "---------------------------" & vbCrLf  
            'Console.WriteLine(msg)  
        Next  
        'Response.Write("</Pre>")  
        If NortonLocation = "" Then  
            MsgBox(SearchDir & "   " & searchFile & " not found")  
        Else  
            Return NortonLocation  
        End If  
    End Function  

in the function: "Function GetDirectoryLocation" the error is in the following line for "ar"

" For Each fi In ar"
the eror being: "Object reference not set to an instance of an object" located against "ar"

it works fine on windows 10 but creates this error when running under windows 11

any assistance to fix this would be most appreciated

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,585 questions
{count} votes

Accepted answer
  1. MotoX80 32,081 Reputation points
    2022-09-05T13:48:05.47+00:00

    When you encounter a directory that you do not have access to, your try/catch caught the error but you did not return ar.

    Note the @MotoX80 comments.

    The code is only finding the last occurrence of whatever file that you are looking for. Is that what you want?

    Module Module1  
        Sub Main()  
            Dim x As String = ""  
            Dim arFiles As String  
            arFiles = GetDirectoryLocation("c:\", "EvanInHallway.jpg")  
            MsgBox(arFiles)  
        End Sub  
        Public Function DoRecursiveSearch(ByVal DirPath As String,  
    Optional ByVal FileSearchPattern As String = "*.*",  
    Optional ByVal FolderSearchPattern As String = "*",  
    Optional ByVal Recursive As Boolean = True) As ArrayList  
      
            ' Specify the directories you want to manipulate.  
            Dim di As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(DirPath)  
            Dim ar As New ArrayList  
            Try  
                ' Get only subdirectories   
                Dim dirs As System.IO.FileSystemInfo() = di.GetDirectories(FolderSearchPattern)  
                Dim files As System.IO.FileSystemInfo()  
                Dim diNext As System.IO.DirectoryInfo  
                Dim fiNext As System.IO.FileInfo  
      
                files = di.GetFiles(FileSearchPattern)  
                For Each fiNext In files  
                    Console.WriteLine(fiNext.Name)  
                    ar.Add(fiNext)  
                Next  
      
                If Recursive Then  
                    For Each diNext In dirs  
                        ar.AddRange(DoRecursiveSearch(diNext.FullName, FileSearchPattern, FolderSearchPattern, Recursive))  
                    Next  
                End If  
      
            Catch e As Exception  
                Console.WriteLine("The process failed: {0}", e.ToString())  
            End Try  
            Return ar                     ' @ MotoX80 you need to return the arraylist   
        End Function  
      
        Public Function GetDirectoryLocation(ByVal SearchDir As String, ByVal searchFile As String) As String  
            '"C:\Program Files\Norton Security\"  
            Dim NortonLocation As String = ""        ' @ MotoX80  undefined variable   
      
            Dim msg As String  
            Dim ar As New ArrayList  
            Dim fi As System.IO.FileInfo  
            'ar = GetFiles(Server.MapPath("."), "*.aspx")  
            ar = DoRecursiveSearch(SearchDir, searchFile)  
            'Response.Write("<Pre>")  
      
            For Each fi In ar  
                NortonLocation = fi.FullName  
                'msg = msg & "File Path: " & fi.FullName & vbCrLf  
                'msg = msg & "File Size: " & fi.Length & vbCrLf  
                'msg = msg & "Last Modified: " & fi.LastWriteTime & vbCrLf  
                'msg = msg & "---------------------------" & vbCrLf  
                'Console.WriteLine(msg)  
            Next  
            'Response.Write("</Pre>")  
            If NortonLocation = "" Then  
                MsgBox(SearchDir & "   " & searchFile & " not found")  
            End If  
            Return NortonLocation        ' @ MotoX80 we only return the first occurrance????   
        End Function  
    End Module  
      
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. x38class-9313 141 Reputation points
    2022-09-06T05:49:23.15+00:00

    Thanks for taking an interest in my project
    The changes you made were applied & it works fine.

    Thank you, much appreciated

    0 comments No comments