我正在用面向对象的LiveCharts构建一个WPF数据可视化工具。我有一个名为SectionsCollection的SectionsCollection对象,需要在数据变化时重新加载它。在重新分配到SectionsCollection之前,我运行以下代码段。
try
{
if (SectionsCollection != null && SectionsCollection.Count > 0)
{
SectionsCollection.Clear();
}
}
catch(Exception e)
{
Status += "Error in clearing SectionsCollection.\n"+e;
}
SectionsCollection = new SectionsCollection();以下错误在SectionsCollection.Clear();行上间歇性地发生,标签NullReferenceException发生。
Exception thrown: 'System.NullReferenceException' in LiveCharts.Wpf.dll
Additional information: Object reference not set to an instance of an object.如果我检查SectionsCollection不是空的,也不是空的,为什么会出现这个错误?
对于VisualsCollection和SeriesCollection类型,似乎也会出现此错误。
发布于 2017-12-12 04:06:02
尝试添加标志以防止不必要的多个exec。在你的过程中:
bool collIsBusy = false;
try
{
if (SectionsCollection != null && SectionsCollection.Count > 0 && !collIsBusy)
{
collIsBusy = true; //flag to prevent throttling multiple executions
SectionsCollection.Clear();
collIsBusy = false;
}
}
catch (Exception e)
{
Status += "Error in clearing SectionsCollection.\n" + e;
}
SectionsCollection = new SectionsCollection();https://stackoverflow.com/questions/45597594
复制相似问题