Please help with NoE

Some Windows users get a NilObjectException when trying to download an update file. Of course, it works fine for me on all my test machines :confused:

The update info has been successfully downloaded and presented in a modal window.
As soon as the user clicks the Download button, the error immediately appears.

The Download button disables itself (you can see that that’s happened) and calls GetFile, where you see I’ve already added a bunch of nil checks:

If f <> nil then
  If LblDownloading <> nil then
    LblDownloading.Visible = True
    If ProgressBar1 <> nil then
      ProgressBar1.Visible = True
      // Async Send, all results handled by events
      UpdateSocket.Send("GET",DownloadURL, f,15)  // (last parm is timeout in secs)
      Return
    Else
      MsgBox "ProgressBar is Nil!"
    End
  Else
    MsgBox "Label is Nil!"
  End
Else
  MessageBox "File "+f.NativePath+" is nil!"
End

Self.Close

UpdateSocket is a URLConnection added to the window in the IDE. It has event handlers for Error, FileReceived, and ReceivingProgressed.

f is SpecialFolder.UserHome.Child(“Downloads”) - could this be a permissions issue? I do check for f being Nil, but maybe URLConnection throws an NoE if it can’t write?

I should have added: Xojo 2021r2.1

Hi Julia.

If the exception was in the code executed by the button I would have expected the button to be listed in the stack trace. However, I have seen a bug logged for stack traces not working correctly when triggered from modal alerts so may be you be hitting that bug and it is excluding some important information.

Are you checking for SpecialFolder.UserHome.Child(“Downloads") being Nil?

Unfortunately, I can’t comment on URLConnection as we replaced our comms code with cURL via the MBS plugin.

The call to f.NativePath will raise a NOE if f=Nil.

4 Likes

Ah, thank you, so obvious now :slight_smile:

SpecialFolder.UserHome.Child(“Downloads") is what’s being passed to GetFile as “f”, so yes, it’s being checked in GetFile, but as @Andrew_Lambert points out, I then proceed to blithely use f in my MessageBox even if it’s Nil.

This still leaves the question unanswered as to why SpecialFolder.UserHome.Child(“Downloads") would ever be Nil - could it be a OneDrive thing, or a permissions thing, or
? Maybe Desktop would be safer, I dunno. I guess I can always throw up a dialog and let the user choose.

maybe try .Exists first
or use
.TemporaryFile
or path
SpecialFolder.Temporary

your folderitem is something like this?
f=SpecialFolder.UserHome.Child(“Downloads”).Child(“Update.exe”)
i am confused why you say it points to Downloads

user have Windows 11^^ i see round edges in screenshot

Yes, you’re correct, f is a child of Downloads, not Downloads itself.

Dim f As FolderItem
f=SpecialFolder.UserHome.Child("Downloads")
f=f.Child(UpgradeFileName)
UpgradeDialogWindow.ShowInfo(UpdateInfo,DownloadURL,f)

and if it is still an issue, there appears to be no check to see if UpdateSocket is nil
 ?
Do you have any DoEvents calls ?

i would rewrite
Dim path As FolderItem=SpecialFolder.UserHome.Child(“Downloads”)
if path = nil then Message
if path.exists then
else
message or create folder
endif
Dim f As FolderItem
f=path.Child(UpgradeFileName)

Windows does give the user an option to relocate the Downloads folder. I don’t know how many do this but it’s a theoretical possibility.

It would be nice if there were actually a SpecialFolder.Downloads. Some years ago I read a thread of VB users who were looking for the same thing. With a little help from my friends, I adapted a VB declare to Xojo, which has been so far, so good with me.


Var f as folderitem

Var myID as Ptr=COM.IIDFromString("{374DE290-123F-4565-9164-39C4925E467B}")//"Official" Downloads Folder
Var myCode as Int32
Var myPath as WString
Soft Declare Function SHGetKnownFolderPath Lib "Shell32" (theId as ptr, flags as Int32, ByVal theToken as Int32, ByRef thePath as WString) as Int32
Soft Declare Sub CoTaskMemFree Lib "ole32" (byVal hMem as WString)
myCode=SHGetKnownFolderPath(myID,0,0, myPath)

if myCode=0 then
  f=New FolderItem(myPath,FolderItem.PathModes.Native)
end if

//Release the memory.  Not sure if I really need to pass a memoryblock instead of WString, but hey, it doesn't crash.
CoTaskMemFree(myPath)

Return f
1 Like

It could also be nil if the user is in a domain, are not allowed to download things from the internet and the whole folder has been removed.

1 Like

I had asked the user some time ago if he had

C:\Users<user>\Downloads

and indeed he does:

image

image

so I concluded that that was not the issue.

A small test program sent to him now, however, reveals that while not nil, Downloads does not exist as far as Xojo is concerned, so that any child I designate will be nil.

Pretty sure it’s a OneDrive thing. Easy to fix now that I know the root cause. Thanks to all.

1 Like