对象不为空并且仍然引发异常
答案:它在 else 代码中,但我认为代码没有理由不指向该行。
有人可以提供一个想法,我如何可能在 Invoke 方法中得到“对象引用未设置到对象的实例”:
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
{
....
}
}
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
{
....
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定实际问题是什么,考虑到您的示例不能是执行的代码,但请尝试这样的操作:
另外请确保它实际上不是
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:
Also please make sure that it's not actually whatever is in the
else
part that fails.如果我没记错的话,这个异常可能来自被调用的方法内部。如果您在 AddItem 方法的 else 内放置一个 try/catch 并在 catch 内放置一个断点,您会捕获异常吗?
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?目前尚不清楚给定示例中是否存在错误,但从未检查
tab
是否为null
,但它作为参数传递。它可能是
null
,或者:另外,
inv
在创建后立即检查null
(在 C/C++ 中很好,但在 C# 中不必要)因为它抛出OutOfMemoryException
失败),但它是在检查o
是否为null
之前完成的。It's unclear whether it's a mistake in the given example, or not, but
tab
is never checked fornull
, yet it is passed as the argument.Either it's probably
null
, or:Also,
inv
is checked fornull
right after creating it (Good in C/C++, but unnecessary in C# as it throws anOutOfMemoryException
upon failure), but it is done before checkingo
fornull
.