locked
Control.Invoke throws NullReferenceException RRS feed

  • Question

  • Here is a simplified version of what I'm doing:

    class MyControl: UserControl  
    {  
         //Some contructors  
           
         public enum MyEnum{val1, val2, val3};  
         delegate void MyDelegate(MyEnum newValue);  
         public void MyMethod(MyEnum value)  
         {  
              if(this.InvokeRequired)  
              {  
                   MyDelegate d = new MyDelegate(MyMethod);  
                   this.Invoke(d, value);  //This is where the error occurs  
              }  
              else  
              {  
                   //Other stuff happens  
              }  
         }  
    }         
     
    The exception that is thrown is a NullReferenceException.  d and value are both set (not null). 

    The target site of the exception is {System.Object MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)}

    Any ideas?
    Thursday, February 12, 2009 6:05 PM

Answers

  • Your error is most likely in "the other stuff".  Post that code.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by fyathyrio8 Thursday, February 12, 2009 8:54 PM
    Thursday, February 12, 2009 7:08 PM
  •  That happens when using invoke, it's annoying. All the stuff in the else is being run inside that Invoke and it likes to tell you the error is coming from Invoke instead of going into more detail. Try some breakpoints in the else part to catch what's happening.

    Edit: I never tried this when I ran into the problem, but it may work. Open the Threads window when debugging and check your threads when the error is thrown. Maybe a different thread will show you where the error is really coming from.
    • Marked as answer by fyathyrio8 Thursday, February 12, 2009 8:54 PM
    Thursday, February 12, 2009 8:35 PM

All replies

  • Your error is most likely in "the other stuff".  Post that code.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by fyathyrio8 Thursday, February 12, 2009 8:54 PM
    Thursday, February 12, 2009 7:08 PM
  • If the error was in the "Other stuff" why would this.Invoke be throwing the error?  The reason I neglect to post the rest of the code is that it is quite detailed.  It would probably be more feasible to zip the entire project and email it rather than to post it here.
    Thursday, February 12, 2009 8:32 PM
  •  That happens when using invoke, it's annoying. All the stuff in the else is being run inside that Invoke and it likes to tell you the error is coming from Invoke instead of going into more detail. Try some breakpoints in the else part to catch what's happening.

    Edit: I never tried this when I ran into the problem, but it may work. Open the Threads window when debugging and check your threads when the error is thrown. Maybe a different thread will show you where the error is really coming from.
    • Marked as answer by fyathyrio8 Thursday, February 12, 2009 8:54 PM
    Thursday, February 12, 2009 8:35 PM
  • Go for it.  A little creative link following and you'll find my email address.  Once I find the error, I'll post the relevant code and solution here.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by fyathyrio8 Thursday, February 12, 2009 8:54 PM
    • Unmarked as answer by fyathyrio8 Thursday, February 12, 2009 8:54 PM
    Thursday, February 12, 2009 8:37 PM
  • You guys were right.  It was in the else.  It took me 13 seconds to figure out once I bothered to look outside of the Invoke call.  Thank ya much.

    - Mike -
    Thursday, February 12, 2009 8:53 PM