Weird NilObjectException

So I have this bit of code, where fi is a FolderItem. When it runs, Xojo raises a NilObjectException on the third line.


if fi<>nil then
  if fi.Exists=True then
    if fi.IsReadable=True and f.Length>0 then    '<-------- EXCEPTION OCCURS ON THIS LINE  

fi is not nil, it exists, and it’s readable and writeable, it has a path, it isn’t locked, and it isn’t a directory, there are no odd characters in the name, and LastErrorCode=0 - or at least that’s what the debugger says.

Any thoughts on why I’m getting a NilObjectException?!?

Weird, really weird. This is with Xojo 2015r3.

[quote=222834:@John McKernon]So I have this bit of code, where fi is a FolderItem. When it runs, Xojo raises a NilObjectException on the third line.


if fi<>nil then
  if fi.Exists=True then
    if fi.IsReadable=True and f.Length>0 then    '<-------- EXCEPTION OCCURS ON THIS LINE  

fi is not nil, it exists, and it’s readable and writeable, it has a path, it isn’t locked, and it isn’t a directory, there are no odd characters in the name, and LastErrorCode=0 - or at least that’s what the debugger says.

Any thoughts on why I’m getting a NilObjectException?!?

Weird, really weird. This is with Xojo 2015r3.[/quote]

This isn’t directly related to your problem, but generally you should just open the file and handle failure instead of doing much in the way of pre-flight checks.

Really? Virtually every example I’ve seen always checks for nil before doing things with an object, including folder items.

In this case, even if a Try/Catch catches the error, I don’t understand what Xojo thinks something (presumably fi) is nil.

except you’re not checking FI in both cases
you have F.length

if fi.IsReadable=True and f.Length>0 then '<-------- EXCEPTION OCCURS ON THIS LINE 

FI.isreadable
F.LENGTH

and F might be nil

[quote=222848:@John McKernon]Really? Virtually every example I’ve seen always checks for nil before doing things with an object, including folder items.

In this case, even if a Try/Catch catches the error, I don’t understand what Xojo thinks something (presumably fi) is nil.[/quote]

Checking for Nil is much different than doing preflight checks of Exists, IsReadable, and Length.

Yikes! How incredibly obvious. I’m glad folks like Norman have better eyes than mine.

Thank you! Now I can sleep…:slight_smile: