# Help needed tracking a NilObject in a recursion

**URL:** <https://forum.xojo.com/t/help-needed-tracking-a-nilobject-in-a-recursion/64834>\
**Category:** macOS\
**Created:** [August 2, 2021, 8:00am UTC](https://forum.xojo.com/t/help-needed-tracking-a-nilobject-in-a-recursion/64834 "2021-08-02T08:00:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Piper](https://forum.xojo.com/letter_avatar_proxy/v4/letter/i/ba8739/32.png) [@Ian\_Piper](https://forum.xojo.com/u/Ian_Piper)\
**Post date:** [August 2, 2021, 8:00am UTC](https://forum.xojo.com/t/help-needed-tracking-a-nilobject-in-a-recursion/64834/1 "2021-08-02T08:00:14Z")

</div>

Hi everyone,

I’d like some advice about how to track down a problem I’m having with a recursive folder search. I am using a blog ([Recursion: Emptying a Folder – Xojo Programming Blog](https://blog.xojo.com/2019/02/04/recursion-emptying-a-folder/)) as my source for doing a search for folder. Basically I want to find all instances of node\_modules folders to do some VueJS housekeeping. The code works most of the time but I keep having a NilObjectException. I am using a Try-Catch block but it’s not able to identify the issue. Here is the code where the exception is being raised. It looks to my (relatively inexperienced) eyes as though f.item(n).NativePath is the thing that is a NilObject but I can’t think why, since I’d been using if clauses to identify these strings. The Try-Catch block just raises another NilObjectException. How can I get more information on this to narrow down what is going on?  
Any guidance much appreciated.  
Code below:

```auto
If f.Directory Then
  For n As Integer = f.Count DownTo 1
    If f.Item(n) <> Nil Then
      // If it's a folder, recurse into it
      If f.item(n).Directory And f.Count <> 0 Then
        Dim f1 As FolderItem = f.item(n)
        f1.deleteAllFilesInside
      End If
      // if the folderitem is not empty and the path is not the original folder then add the pathname to the listbox.
      // Check whether the folder name is the one ("node_modules") set in txtFolderName.Text
      Try
        if f.item(n).NativePath.right(Window1.txtFolderName.Text.Length) = Window1.txtFolderName.Text then
          If f.item(n) <> Nil And f.item(n).NativePath <> originfolder Then Window1.lstFolderNames.addRow(f.item(n).NativePath)
        end if
      Catch e As NilObjectException
        MessageBox("Looks like a nil object here:" + f.item(n).NativePath)
        Return
      End Try
    End If
  Next
End if

```

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [August 2, 2021, 8:10am UTC](https://forum.xojo.com/t/help-needed-tracking-a-nilobject-in-a-recursion/64834/2 "2021-08-02T08:10:07Z")

</div>

> [@Ian\_Piper](#):
>
> ```auto
> f1.deleteAllFilesInside
> End If
> 
> ```

Is `deleteAllFilesInside` the name of your recursive method? If so I’d expect you’d need a continue after calling it, thus:

```auto
  f1.deleteAllFilesInside
  Continue
End If

```

since you have completed everything you wanted to do with f1.

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [August 2, 2021, 9:04am UTC](https://forum.xojo.com/t/help-needed-tracking-a-nilobject-in-a-recursion/64834/3 "2021-08-02T09:04:41Z")

</div>

if f is nil then f.directory throws a NilObjectException  
if f1 is nil then f.deleteAllFilesInside throws a NilObjectException  
they are all outside the try…catch

---

<div class="post-metadata">

**Author:** ![Ian\_Piper](https://forum.xojo.com/letter_avatar_proxy/v4/letter/i/ba8739/32.png) [@Ian\_Piper](https://forum.xojo.com/u/Ian_Piper)\
**Post date:** [August 2, 2021, 10:58am UTC](https://forum.xojo.com/t/help-needed-tracking-a-nilobject-in-a-recursion/64834/4 "2021-08-02T10:58:45Z")

</div>

Thanks for the quick replies folks. These made me realise that there were a number of flaws in my code. Now that I’ve resolved those it seems to work fine.
