(Found) Problem with a folderitem file just created not existing?

I’m not sure what is happening to this file. I’m getting a strange NilObjectException as in it doesn’t exist.
It exists and in fact it was rewritten just moments before, and every check like does it exist is yes.

I redid my App with a lot API2 and have a couple of versions along the way.
The version with stuff like file.IsFolder is an older and current version.
The current version is more complete.

I haven’t put in my App.FileErrorTest the new version including stuff like Try – Catch err As IOException yet.

Before the file even gets here, it has had a few checks like

If YWrdDictF <> Nil And YWrdDictF.Exists Then

The file (tObj) is not a folder, but a file.
Here’s the code which is causing the error:

If isDir = False And tObj.IsFolder = True Then//fails here as doesn't exist. Return Nil ElseIf isDir = False And tObj.IsFolder = False Then Return tObj End If Return tObj

It doesn’t go to the next test which is correct for it.
Suggestions.

need to see the lines above that where you create tObj

because if tObj IS indeed nil you will get a nil object exception on the line you indicated


if tObj is nil then
 BREAK /// since I have no idea what the RIGHT thing to do when tObj IS NIL !!!!!!!
else
  If isDir = False And tObj.IsFolder = True Then//fails here as doesn't exist.
    Return Nil
  ElseIf isDir = False And tObj.IsFolder = False Then
    Return tObj
  End If
end if
Return tObj

It’s a function called

FileErrorTest

Parameters

tObj As folderItem, fType as String, rW as boolean, isDir As Boolean, mMsg As Boolean
Return FolderItem

I rewrote to API2 and it still fails inappropriately but now at the errornumber = 101

the whole function

[code]//rW True = only Read is Required, False = Read Required and Write is Optional
//tObj As folderItem, fType as String, rW as boolean, isDir As Boolean, mMsg As Boolean
//mMsg True if used MsgStrd is wanted to be displayed
'if tObj = Nil Then Return Nil

Var p as new Permissions(tObj.Permissions)

Try
if tObj <> Nil And tObj.Exists = True Then
If isDir = False And tObj.IsFolder = True Then
Return Nil
ElseIf isDir = False And tObj.IsFolder = False Then
Return tObj
End If
End If

#If TargetMacOS or TargetLinux Then
if p.OwnerRead = False then
If mMsg Then MessageBox "Caution with this " + fType + " File: " + tObj.Name +_
EOL + “You Cannot Currently Access It.”
MsgStrd = "Error with this " + fType + " File: " + tObj.Name + EOL + Str(tObj.lastErrorCode) +_
EOL + “You Cannot Currently Access It.”
Return Nil

elseif p.OwnerWrite = False And rW = False then
  If mMsg Then MessageBox "Caution with this " + fType + " File: " + tObj.Name +_
  EOL + "You May Need to Change" + EOL + "this File's Permission to Read & Write."
  MsgStrd = "Error with this " + fType + " File: " + tObj.Name + EOL + Str(tObj.lastErrorCode) + "this File's Permission to Read & Write."
  Return tObj
end if

#EndIf

Catch err As IOException

If err.ErrorNumber <> 0 Then//tObj.NoError = <> tObj.NoError Then

if err.ErrorNumber = 100 Then
  If mMsg Then MessageBox "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Destination does not exist."
  MsgStrd = "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Destination does not exist."
  Return Nil
  
ElseIf err.ErrorNumber = 101 Then
  If mMsg Then MessageBox "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Destination does not exist."
  MsgStrd = "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Destination does not exist."
  Return Nil
  
Elseif err.ErrorNumber = 102 Then
  If mMsg Then MessageBox "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Access denied"
  MsgStrd = "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Access denied"
  Return Nil
  
Elseif err.ErrorNumber = 103 Then
  If mMsg Then MessageBox "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Out of memory"
  MsgStrd = "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Out of memory"
  Return Nil
  
Elseif err.ErrorNumber = 104 Then
  If mMsg Then MessageBox "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  File in use"
  MsgStrd = "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  File in use"
  Return Nil
  
Elseif err.ErrorNumber > 0  Then
  If mMsg Then MessageBox "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Report This Error to Management"
  MsgStrd = "Error with this " + fType + " File: " + tObj.Name + EOL + err.ErrorNumber.ToString + ":  Report This Error to Management"
  Return Nil
end if

End if
End Try

If isDir = False And tObj.IsFolder = True Then
Return Nil
ElseIf isDir = False And tObj.IsFolder = False Then
Return tObj
End If
Return tObj[/code]

I just pasted the above code into my “Not too much API 2” version and it failed.
I thought the version was safe for this,
so I reverted to the saved (that was the only thing I did) and it worked.

I wondered if it had something to do just with ioexception, but that’s already in it for the TextOutputStream.

[code]Try
t = TextOutputStream.Create(nwf)
t.close
If f.Exists Then f.Remove

nwf.MoveTo(f)

Catch err As IOException[/code]

[code]/rW True = only Read is Required, False = Read Required and Write is Optional
//tObj As folderItem, fType as String, rW as boolean, isDir As Boolean, mMsg As Boolean
//mMsg True if used MsgStrd is wanted to be displayed
'if tObj = Nil Then Return Nil

Var p as new Permissions(tObj.Permissions)
dim bOK as boolean = true
if tObj = Nil Then
bOK = false
MsgStrd = "File is nil’
end if

if bOK and not tObj.Exists then
bOK = false
MsgStrd = “File does not exist”
end if

if bOK and isdir <> tObj.IsFolder then
bOK = false
MsgStrd = “Folder check failed”
end if

if BOK and p.OwnerRead = False then
bOK = false
MsgStrd = “Cannot read owner”
end if

if bOK and (p.OwnerWrite = False And rW = False) then
bOK = false
MsgStrd = “Read Write permissions failed”
end if

mMsg = not bOK

if mMsg then
msgbox MsgStrd
return nil
else
return tObj
end if[/code]

What happens if you trace through this?
And Im not familiar with permissions… is OWNERwrite correct here?

Oh, and

Have you got iCloud (spit) turned on with full synchronisation?
I swear it trips me up now and then.
So much so that in some places in my code I have taken to introducing a DelayMBS 1 before trying to use a newly created file, just in case it is busy being transported somewhere else by iCloud, or ‘examined’ by an over eager virus checker.

Edit: Should test tObj for Nil BEFORE attempting to get permissions from the object.
In your code (and mine above which I cannot now edit), getting permissions first would cause an immediate nilobject exception if the object was nil

Forgot to add this is on a PC.
The below line is the test for entering this function.

I also know this happens only because the file is removed (deleted) and replaced.

No further updates except I have the time to find out which part of API 2 did those, so I am redoing it from a safe point.

I found the problem. Something is wrong with moving from from

FolderItem.Delete[/code] to [code]FolderItem.Remove

As soon as I changed the code the method failed.

Suggestions on how to give Feedback?

file a bug report with as many details as you can and if possible a reproducible case