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

Object reference not set to an instance of an object error wpf nunit testing

为什么我在尝试清除可观察集合的值时收到错误“对象引用未设置到对象的实例”?

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

根据您提供的有限代码,当您尝试访问计数时,testcollection 似乎为空。试试这个:

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

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

Dispatcher.BeginInvoke();

尝试以下 link、

link2