if f.exists = false generates NilObjectException

Is there something wrong with the following statement?

if f.exists = false then

It generates NilObjectException when the file is known to not exist.
This has been a standard way to avoid NilObjectException and is under folderitem.exists

I would expect f is nil when the exception is raised!?

No. Just to clear Nil usually shows in the debugger such that you have the name and then Nil, but here I have folderitem underlined and I can look inside and the program gave it a path which doesn’t exist. I am trying to isolate text from what I call a partial path that gets turned into a folderitem, but this one doesn’t exist definitely.

No, that’s not a standard way to avoid NilobjectException. You’ll need to check for nil first:

if f<>nil and not(f.exists) then
…code…
else
…f was nil OR file does not exist…
end

I tried this code that doesn’t work.

       if Nil <> App.PlayWordP or Not(App.PlayWordP.Exists) Then

I think that is what you wrote.

You can’t compare nil to something, you have to compare something to nil. Also, it has to be AND, not OR:

if App.PlayWordP<>nil AND Not(App.PlayWordP.Exists) Then

No, you need to use an “And” there.

If App.PlaywordP <> Nil And Not App.PlayWordP.Exists Then

If you use “Or” then both sides are evaluated. With “And”, then 2nd side is evaluated only when App.PlaywordP <> Nil.

I usually do that. In this case it doesn’t matter for coding. I know that it isn’t nil since it has a path. My problem is the NilObjectException being raised on the line.

the statement only has two items.
F and FALSE
one of those MUST be nil to get that error, no matter what you say.

FALSE is a reserved word. You shouldn’t be able to create an object called FALSE, so let’s assume you didn’t.

Try this:

[code]if f <> nil then
if not f.exists then
msgbox "File does not exist "
end if

if f.exists = false then 'IF this line causes an exception, you did something very odd with FALSE
msgbox "File does not exist… second test "
end if

else
msgbox "File is null "
endif[/code]

if (Nil <> App.PlayWordP) AND App.PlayWordP.Exists Then // the folder item is not nil & the item exists

OR stated the other way so you deal with the BAD case

if (Nil = App.PlayWordP) OR NOT(App.PlayWordP.Exists) Then // the folder item is nil OR the item DOES NOT exist

should be fine

Sorry. I got lost in my variable names and clicked one I thought was the same ‘f’ when it wasn’t.
Since I’d already tested the variable App.PlayWordP and it had returned Nil from the test, the code I needed was/is

if Nil = App.PlayWordP Then, but I’d used f and wasn’t paying attention to names only to ‘exists’. It dawned on me eventually to check the name. Thanks

I’m also facing with this particular issue, I got Nil Object Exception when I delete the folder from its path:

#Pragma NilObjectChecking OFF try SourceXMLFile = SpecialFolder.Documents.Child("Invoices").Child(Selector(1)).child(RFC).Child(YearRow).child(Month).child(RazonORFC).Child(folioUUID+".xml") SourcePDFFile = SpecialFolder.Documents.Child("Invoices").Child(Selector(1)).child(RFC).Child(YearRow).child(Month).child(RazonORFC).Child(folioUUID+".pdf") If (Nil = SourceXMLFile) or Not(SourceXMLFile.Exists) Then MsgBox "El folder esta vacio o el Item no existe" Else Msgbox "Si existe el archivo" End If

Even I tried with Pragma without success, When I do NilObjectChecking FALSE
The Program Crashes

Any clues?

You can either use the pragma #Pragma Break On Exceptions Off or simply resume in the IDE to test the Try/Catch block.

You are great Sir!, Thanks so much

I found very often much more readable to compare against an object type with the IsA operator.

if f isA FolderItem then

[quote=319484:@Massimo Valle]I found very often much more readable to compare against an object type with the IsA operator.

if f isA FolderItem then [/quote]

Agree, except I use the generic “object”. That way if I decide to change the class of the thing I’m testing later, the code will still work.

if f isa object then ...

[quote=319485:@Kem Tekinay]Agree, except I use the generic “object”. That way if I decide to change the class of the thing I’m testing later, the code will still work.

if f isa object then ... [/quote]
except if the item has to be a specific type then this code doesn’t help you in that regard

say you have a super and several subclasses and you do

dim f as mySuperClass
f = new oneSubclass
... a bunch of other code ...
if f isa object then ...

all you know is that it is an object when maybe you needed to know it is a specific subclass
That said having to know its a specific subclass is more likely a design problem with the class hierarchy

And you probably dont want the “isa Object” check with “variant.ObjectValue” since that could be any object

I completely agree with Norman on that!

isA Object is kind of evil.

I thought we were talking specifically about if not (f is nil) then.... I was recommending ... isa object ... as a more readable alternative to that. Naturally if you have to test for a specific type of object, you should do that.

stylistically I would not write

if not (f is nil) then
  // code that I want to run
else
  // deal with nil
end if

Why not ?
The “good code path” is buried in the “then” part and ends up possibly deeply nested inside if’s
So I tend to test for criteria that I reject like

if f is nil then
  // deal with nil
  // possibly a return
end if

or

if f is nil then
  // deal with nil
else

end if

so the “good code path” is not buried in the “Then” portion and possible having a ton of trailing “else’s”

And yes I often return early so I can write

if f is nil then
  // deal with nil
  // return
end if
// good code

It amounts to asserting early often & bailing out as soon as possible
And it will offend anyone who believes heavily in “Enter once leave once”