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?!?
[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.
[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.