1

Hello I am having a strange problem in .net 4.0. I am calling the following code from my main form thread and I am getting an "object reference not set to an instance" exception on variable MainForm.NumberOFWindows(although it is initialized) and I can not figure out why. The really strange thing for me is that the exception is not being catched by the try catch exception clause either. Can someone please shred some light to my problem? Why does the exception occurs and why it is not being catched by the try catch clause?

try
{
    MyThread = new Thread(delegate() 
    { 
        Notify.UserInput(this,ref MainForm.NumberOFWindows);
    });
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message);
}
Max
  • 12,622
  • 16
  • 73
  • 101
mpc
  • 67
  • 1
  • 9
  • @MaxMommersteeg What is that supposed to achieve? `MyThread` is presumably a property. – Ant P Mar 26 '14 at 11:17
  • The try/catch won't catch it because it's not being run at that point. Where is the code that runs MyThread? – Matthew Watson Mar 26 '14 at 11:18
  • To catch the error you need to put a try catch inside the delegate – Johan Mar 26 '14 at 11:18
  • 1
    If it isn't declared then the code won't even compile. Which it obviously does, because this is a runtime exception. – Ant P Mar 26 '14 at 11:20
  • MainForm should also never be accessed from anything except the Main Thread. – Aron Mar 26 '14 at 11:21
  • How to access the main form from another thread: http://stackoverflow.com/questions/3923300/accessing-form-window-in-another-thread – JustAndrei Mar 26 '14 at 11:23
  • Hi and thanks for the replies. 1) The NumberOFWindows is an int primitive variable and it is initialized. also if i print the value before I get the exception it prints out some value. 2) Yes this is a run time error. 3) The MyThread is a thread class. The new thread will run UserInput function from the Notify class – mpc Mar 26 '14 at 11:28
  • I don't understand why people marked it as a duplicate of that particular question. They are different because here OP's problem is not understanding what is NullReferenceException but understanding why it happens in his particular multithreading case – Andriy Tylychko Mar 26 '14 at 18:22

2 Answers2

1

why it is not being catched by the try catch clause?

Because it happens in different thread with different stack than your try/catch block

Why does the exception occurs despite MainForm.NumberOFWindows is initialized?

Just a guess, maybe because you check it's initialized in the main thread and then destroy it somehow before new thread takes control and tries to access it (thread creation takes some time)

Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112
  • Exception handling in threads: http://stackoverflow.com/questions/1554181/exception-handling-in-threads – JustAndrei Mar 26 '14 at 11:22
  • So if i understand you correctly the exception I am getting from the delegate is actually happening on another thread although I have not yet called the MyThread.Start() function, yes? – mpc Mar 26 '14 at 11:36
  • It will happen in another thread when you call `MyThread.Start()` – Andriy Tylychko Mar 26 '14 at 11:56
  • Ok thanks. How about the int variable MainForm.NumberOFWindows. If I print the variable before the delegate it prints out a value, i.e. 1. What could the problem here? Is it because I try to create a reference to a variable by referencing the MainForm first or do you think it is something else? – mpc Mar 26 '14 at 13:31
  • it doesn't matter what MainForm is outside of the delegate. (You're are interested in MainForm, not NumberOfWindows field because it's MainForm that is destroyed and doesn't reference an instance anymore). Because the delegate will be called in another context and later. Check MainForm **in** the delegate just before accessing it. – Andriy Tylychko Mar 26 '14 at 14:49
  • Alright I will and report back if it solves my problem! – mpc Mar 26 '14 at 15:35
  • After putting the check if(MainForm != null) inside the delegate the problem has yet to appear. I will keep checking just in case but perhaps the problem has disappeared. – mpc Mar 27 '14 at 12:13
0

why it is not being catched by the try catch clause?

if you want this you have to put the try catch block into the delegate like this

    MyThread = new Thread(delegate() 
    { 
         try
           {
           Notify.UserInput(this,ref MainForm.NumberOFWindows);
           }
           catch (Exception ex)
           {
            MessageBox.Show(ex.Message);
           }
    });
abrfra
  • 634
  • 2
  • 6
  • 24