title: Searching By File Extensions VB.NET
body: <p>Hi all i have been trying to search a specified directory and all sub directories for all files that have the specified file extension. However the inbuilt command is useless as it errors up and dies if you dont have access to a directory. So here's what i have at the moment:</p>
  
  <pre><code> Private Function dirSearch(ByVal path As String, Optional ByVal searchpattern As String = ".exe") As String()
      Dim di As New DirectoryInfo(path)
      Dim fi As FileInfo
      Dim filelist() As String
      Dim i As Integer = 0
      For Each fi In di.GetFiles
          If System.IO.Path.GetExtension(fi.FullName).ToLower = searchpattern Then
              filelist(i) = fi.FullName
              i += 1
          End If
      Next
      Return filelist
   End Function
  </code></pre>
  
  <p>However i get an "System.NullReferenceException: Object reference not set to an instance of an object." when i try to access the data stored inside the filelist string array.</p>
  
  <p>Any idea's on what im doing wrong?</p>