the following code is giving me a Null Reference Exception on the _items.Clear()
step, and I can't work out why.
public void PlotDate()
{
if (_data == null) return;
_items.Clear();
_items.Add(new Graph
{
Name = "by date",
Items = _data.GroupBy(e => string.Format("{0:yyyy-MMM}", e.Date))
.Select(g => new Item { Key = g.Key, Value = g.Sum(x => x.Size) })
.OrderByDescending(e => e.Value)
.ToList()
});
}
_items
is a bindable collection of Graphs which are displayed in a xaml frontend. In this instance the graph just shows a pie chart of the sum of all sizes in a series of dates. In the class, there are a few of these methods for showing different data which are almost identical, they basically just group the data differently.
The weird thing is that the first one always loads just fine, whichever one I pick, but then when I try to open a second one it throws the exception. When the view is first opened it also runs a method that populates _data
and runs _items.Clear
, so it works fine when _items
is empty.
I've put breakpoints wherever I can think of to try and see what's going on but nothing is being set to null. Right before the _items.Clear
step runs, _items
has a count of 1, then it throws the exception immediately after the count goes down to 0.
I've also tried using _items.RemoveAt(0)
, and putting it after the new graph is added and it does the same thing.
What's going on??
Edit: I'll include this part which might be useful. The display on the UI binds to Items, which is determined by this so it might be where the issue is. I can't get any deeper though, looking at the definition for NotifyOfPropertyChange doesn't yield any useful information.
partial void OnItemsChanged();
public BindableCollection<Graph> Items
{
get { return _items; }
set
{
if (value == _items)
return;
_items = value;
OnItemsChanged();
NotifyOfPropertyChange("Items");
}
}