NILObjectException while Download files through a Thread

Hi all!

I have a One dimensional Array called “Enlaces”, where I Store a lot of links in order to Download it.

This is my code:

  For i as Integer = 0 to Enlaces.Ubound
    DownloadXMLTEMP = SpecialFolder.Desktop.Child("NewCFDIS").Child(Cstr(i)+".xml")
        if Downloader.Get(Enlaces(i), DownloadXMLTEMP,15) Then
    else
      if Downloader.Get(Enlaces(i), DownloadXMLTEMP,15) Then
      end if
    End If
    
  Next
  thrDownloader.kill

When I ran it on Main thread, in a normal method with a Button
I got no problem, I can download all links without problem, But I can’t do that. If I run this code on main thread. I can’t access to the “UI” until the downloads are finished.

So I need to run it on a thread, so, when I do that It gave me this error: “An Exception of class NilObjectException was not handled. The Application must shut down”
And when it happens I can’t do nothing.

I tried to solve it using #Pragma NilObjectChecking Off
and tried to catch it try catch err as NilObjectException ErrorText = err.Message

And I got the same error.

Has it happened to someone here?
What Am I doing wrong?

Also I checked all Links and all exists.

For me this would be more logical

[code]For i as Integer = 0 to Enlaces.Ubound
DownloadXMLTEMP = SpecialFolder.Desktop.Child(“NewCFDIS”).Child(Cstr(i)+".xml")
if Downloader.Get(Enlaces(i), DownloadXMLTEMP,15) Then
//// all is ok
else
/// error message ??
End If

Next
thrDownloader.kill[/code]

[quote=314755:@Axel Schneider]For me this would be more logical

[code]For i as Integer = 0 to Enlaces.Ubound
DownloadXMLTEMP = SpecialFolder.Desktop.Child(“NewCFDIS”).Child(Cstr(i)+".xml")
if Downloader.Get(Enlaces(i), DownloadXMLTEMP,15) Then
//// all is ok
else
/// error message ??
End If

Next
thrDownloader.kill[/code][/quote]

Well In fact I wrote this If inside the other IF preventing the file isnt being downloaded, but also tried as you say and almost get the same error :frowning:

Turn on Break On Exceptions in the Xojo IDE
Then run your code and when it hits the nil object exception please look at the error & message
It might just tell you whats wrong

And no turning of nil object exception checking “wont make it work”
If a nil object was encountered and you had set this pragma this way your app would just crash without the warning

[quote=314759:@Norman Palardy]Turn on Break On Exceptions in the Xojo IDE
Then run your code and when it hits the nil object exception please look at the error & message
It might just tell you whats wrong

And no turning of nil object exception checking “wont make it work”
If a nil object was encountered and you had set this pragma this way your app would just crash without the warning[/quote]
I also tried with this. And the Ide doesn’t show the error.

But I happy cuz, apparently I discovered the way to make a queue using the complete event of the HTTPSecureSocket
And download all links stored on my array.

Now I need to investigate how to refresh the cookies or session.
Cuz passed certain number of files, Then stores but says “Object moved”

[quote=314789:@Gerardo García]I also tried with this. And the Ide doesn’t show the error.

But I happy cuz, apparently I discovered the way to make a queue using the complete event of the HTTPSecureSocket
And download all links stored on my array.

Now I need to investigate how to refresh the cookies or session.
Cuz passed certain number of files, Then stores but says “Object moved”[/quote]
Wooooo hooooo!!! Finally it works. making a Queue, parsing the array.

and Using the DownloadComplete event of HTTPSecureSocket

example:

  if NumDownloaded < Enlaces.Ubound Then
    DownloadedLinks = False
    NumDownloaded = NumDownloaded + 1
    CookieJar                        ///EXPERIMENTAL---- OBTIENE COOKIE
    SetupDownloadSocket 
    

    DownloadXMLTEMP = SpecialFolder.Desktop.Child("NewCFDIS").Child(Cstr(NumDownloaded)+".xml")
    Downloader.Get(Enlaces(NumDownloaded), DownloadXMLTEMP) 
 [b]       'RemoveSpacesXML1(Cstr(NumDownloaded)+".xml",DownloadXMLTEMP)[/b]
    
    
    DownloadStatus = "Downloading: " + Cstr(NumCFDIDescargado) + " of " + Cstr(Enlaces.Ubound)
    
  else
    DownloadedLinks = True
  End If
  

As you can see I comment this: 'RemoveSpacesXML1(Cstr(NumDownloaded)+".xml",DownloadXMLTEMP)
This is a function that I made in order to remove spaces, tabulators, etc ignorer to edit the XML downloaded and then save it again in a different directory.

But I realized of one thing. When the DownloadComplete run, It creates a “Blank file”, so then It connects and finally It got a working XML file.

But if I parse the XML file before the size is different to 0 It will not create the desired XML.

How can I detect when the XML is completed in order to parse it?
Thanks