Answer: it was in the else code, but I thought there is no reason that code wouldn't point to that line.
Can someone offer an idea how it could be possible that开发者_如何学编程 I get "Object reference not set to an instance of an object" on this at Invoke method:
delegate void t(tabdescriptor tab);
internal void AddItem(tabdesciptor tab)
{
if (InvokeRequired)
{
t inv = new t(AddItem);
if (inv != null && tab!= null)
Invoke(inv, tab);
}
else
{
....
}
}
I'm not exactly sure what the actual issue is considering your example cannot be the code that executes, but please try something like this:
internal void AddItem(tabdesciptor tab)
{
if (InvokeRequired)
{
Invoke(new Action<tabdescriptor>(AddItem), tab);
}
else
{
//...
}
}
Also please make sure that it's not actually whatever is in the else
part that fails.
If I remember correctly, this exception could be coming from inside the invoked method. If you place a try/catch inside the else of the AddItem
method and a breakpoint inside the catch, do you catch the exception?
internal void AddItem(tabdesciptor tab)
{
if (InvokeRequired)
{
t inv = new t(AddItem);
if (inv != null && tab!= null)
Invoke(inv, tab);
}
else
{
try
{
....
}
catch
{
// breakpoint here
}
}
}
It's unclear whether it's a mistake in the given example, or not, but tab
is never checked for null
, yet it is passed as the argument.
Either it's probably null
, or:
Also, inv
is checked for null
right after creating it (Good in C/C++, but unnecessary in C# as it throws an OutOfMemoryException
upon failure), but it is done before checking o
for null
.
精彩评论