Calling Invoke on a Form results in NullReference

G

Guest

I have a program which dispatches a time-consuming process to a new thread,
which sends an event when it is done (I have a custom eventargs class to
store the result). The eventargs contains an array of Bitmaps. If I post
the event with only one Bitmap in the array, it works fine. Otherwise, I get
a nullreference in the Invoke inside the form's event handler.

Inside mainform:

delegate void RadarDownloadCompleteEventHandler(object sender,
ImageDownloadCompleteEventArgs e);

void RadarHandler(object sender, ImageDownloadCompleteEventArgs e) {

if(InvokeRequired) {
Invoke(new RadarDownloadCompleteEventHandler(RadarHandler), new object[]
{sender, e});
return;
}
etc
}

If e's array contains 1 image, it works. More than that, and I get a
nullreference (though the debugger shows nothing is null).

Bug?
 
G

Guest

I figured it out by separating the actual event handler into a stub that only
invokes the real method (using one of the variables inside the eventargs),
and the real method)...the null reference was there but instead of showing up
in Invoke it showed up where it actually was.
 
C

Chris

If you're running in debug mode, when you get an exception, the stack
trace should tell you exactly where the error is.

Chris
 
G

Guest

No. It showed me the Invoke call as the error. When I separated the method
and the event handler, it showed me where the error really was.
 
C

Chris

Did you iterate through all the exceptions using the InnerException
property? Anyway, glad you found the problem.

Cheers,
 
G

Guest

I thought toString also included any innerExceptions. I first encountered
the problem via my unhandled exception handler which uses toString and also
did tell me anything besides the nullreference off invoke. Thanks for your
assistance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top