对象引用未设置为对象错误wpf nunit测试的实例



当我试图清除可观察集合的值时,为什么会出现错误"Object reference not set to a instance of a Object"?

if(testcollection.Count>0) testcollection.Clear();

如果testcollection的值不等于null,那么这可能是因为跨线程问题。请尝试在Dispatcher的帮助下实现此功能。

Dispatcher.BeginInvoke();

尝试以下链接,

链接1

链接2

根据您提供的有限代码,当您试图访问计数时,testcollection看起来是null。试试这个:

if (testcollection != null && testcollection.Count > 0) testcollection.Clear();