Thread NilObjectException in method from other object

Hello, I have a problem with Thread. I will tell you it on example.
Webpage1.Method1
Webpage1.Thread1
Webpage1.Button1
Modul1.Method2

What works? When in webpage1.Button1.Action I run Modul1.Method2 and in Method2 I refer to webpage1.Method1.
What doesn’t work? When in webpage1.Button1.Action I run Thread1.Run where I run Modul1.Method2 and in Method2 I refer to webpage1.Method1.
It’s the same, but firstly I run Method2 in Button.Action and then in Thread.Run.
NilObjectException write it on line with Webpage1.Method1? Why, please?

Here is easy example with this problem:
drop box link

Refactor your code to make the windows self-contained. There is a pattern called MVC (model-view-controller), which you can apply here.

You let the windows be the UI only. Most functionality is delegated to the model part of the app. Which then gives the necessary information back to the window.

I don’t have any experience with Xojo web. But I would think that MVC is universal.

It’s not completely. The problem here is that requests from a browser inherently know which session they belong to, while a user created thread does not. Calling any code which wishes to touch the controls in a session will fail without that connection.

If you need a thread which is connected to the current session, use WebThread instead. Keep in mind that these thread/session links are WeakRefs, which means that if the session goes away, you’ll still get exceptions if you try to access any webpages or controls. If you want to keep the session alive during the thread, you could subclass WebThread and create a property for holding a reference to the session, but make sure you set it nil when your thread is done processing, otherwise the sessions will never close.