why nil object exception?

for i as Integer=1 to src.Count if src.Item(i) <> nil then if not src.Item(i).Directory then // Throws a nil object exception

How can this be?

It is Directory which is Nil.

Not much to go on since we can only guess at what src is (looks like it might be a folderitem?). But in general, break it down into temporary variables and examine each in the debugger.

srccount = src.count
for i = 1 to src.count
   srcitem = src.item(i)
   if srcitem<> nil then
      srcdir = srcitem.Directory
      etc...

have tested your code in a listbox dropobject, it works for me

  if obj.FolderItemAvailable then
    dim src as FolderItem = obj.FolderItem
    
    for i as Integer=1 to src.Count
      if src.Item(i) <> nil then
        if not src.Item(i).Directory then
          lb.AddRow src.Item(i).name
        end if
      end if
    next
  end if

I think it’s because I have one thread writing to the folder and another reading it…
this is the reader and the writer may be using it…
hmmm… tricky.

I would highly recommend to put the item result in a local variable.
Calling item() several times is inefficient and between those calls the number of files can change and you get nil if index is out of bounds.

What would you put in the local variable Christian?
The array of Items?

the src.item result.

for i as Integer=1 to src.Count dim item as folderitem = src.Item(i) if item <> nil then if not item.Directory then // Throws a nil object exception // file else // folder end if end if next

You might also consider a CriticalSection (or Mutex if they’re separate programs) to protect the “resource” while you’re accessing it.

[quote=208916:@Brian O’Brien] for i as Integer=1 to src.Count if src.Item(i) <> nil then if not src.Item(i).Directory then // Throws a nil object exception

How can this be?[/quote]

Why don’t you post the entire method instead of two measly lines ?

It is really difficult to assist without all the facts.

Also, what is src exactly ? If it is a folderItem, knowing where it points to could explain things.

[code]Private Sub DeleteFolderContents(f as FolderItem)
dim item as FolderItem
dim i as integer

if f <> nil then
for i=f.Count downto 1
item = f.Item(i)
if item <> nil then
if Item.Directory then
DeleteFolderContents(Item)
end if
Item.Delete
end if
next
end if
End Sub[/code]

YOU HAVE TO DO IT IN REVERSE ORDER !

Yes, for deleting.

But even better is to go forward through folderitem (much faster).
Go forward, collect files to delete in array and than delete them.

see
https://www.monkeybreadsoftware.net/faq-howtodeleteafolder.shtml

If you had posted the entire method as you do now instead of two lines, you would have had the solution yesterday. But it is good you found the issue yourself.

Michel…

I didn’t see the point of posting more than necessary.
Take a closer look at the code and tell me why there should be any reason to believe that an object you just check for nil is now nil when nothing has happened.

 for i as Integer=1 to src.Count
    if src.Item(i) <> nil then   // IT ISN'T NIL
      if not src.Item(i).Directory then  // THIS THROWS A NIL OBJECT EXCEPTION.  NOW IT IS?????  COME ON!

Really I don’t see what difference it makes if i did or didn’t post the rest of the code.
Maybe I should just give up on checking for nil. Seem pointless.
I appreciate the input but I’m missing the relevance.

Cheers!

[quote=208993:@Christian Schmitz]Yes, for deleting.

But even better is to go forward through folderitem (much faster).
Go forward, collect files to delete in array and than delete them.

see
https://www.monkeybreadsoftware.net/faq-howtodeleteafolder.shtml[/quote]
But why would I expect that the nil object exception would not happen again while building the array of folderitems?
I’m just changing one array for another?

[quote=209071:@Brian O’Brien]I didn’t see the point of posting more than necessary.
Take a closer look at the code and tell me why there should be any reason to believe that an object you just check for nil is now nil when nothing has happened.

for i as Integer=1 to src.Count
if src.Item(i) <> nil then // IT ISN’T NIL
if not src.Item(i).Directory then // THIS THROWS A NIL OBJECT EXCEPTION. NOW IT IS??? COME ON!
Really I don’t see what difference it makes if i did or didn’t post the rest of the code.
Maybe I should just give up on checking for nil. Seem pointless.
I appreciate the input but I’m missing the relevance.[/quote]

Brian. The two lines you posted do not indicate what you were doing. Just that you were testing nil, then making an if on Item(i).Directory.

The very reason why you get a nil is because you delete Item. Which appeared only when you posted the final code.

Frankly I do not understand why you find irrelevant that people who have zilch idea of what you are trying to accomplish may ask a hint of information about the rest of the code. It is not as if your code is deemed classified and top secret. Why make it a guessing game when you can provide all the facts ?

I am glad you found the bug yourself, but as I said, if you had cared to post the full method, the answer would have been immediate from people who, don’t forget, are just trying to help.

Checking for nil works, but in your case this is part of the problem…

And the other part is src.Item(i) isn’t constant.

if src.Item(i) <> nil then if not src.Item(i).Directory then
On the first line you get the object at src.Item(i) and check it for nil. On the next line you re-get src.Item(i) and use it but in that tiny interim src.Item(i) could change.

Combine that with src.Item(src.Count+50) => nil. That is, Folderitem.Item(x) just returns nil if x is greater than the available items.

So I speculate that something else is deleting items making the 2nd src.Item(i) return nil because then i > src.Count.

Thanks all.