0

I was trying to implement a loading icon in my WPF application (thru threading) but hit a Null Reference exceptions when I tried to add items into observable collection. I followed the following suggestion but it does not work: How to make ObservableCollection thread-safe?

In short:

This works:

private void Refresh() {
MTObservableCollection <someObject> someTable = new MTObservableCollection<someObject>();
someTable.Add(new someObject());
...
}

This does not work:

private void Refresh() 
{
Task.Factory.StartNew(()=> {
    MTObservableCollection <someObject> someTable = new MTObservableCollection<someObject>();
    someTable.Add(new someObject()); //this line throws null reference for someTable;
});
...
}

The reason I used Task.Factory is because I want to put in a loading icon while this happen.

Community
  • 1
  • 1
Danielle
  • 317
  • 2
  • 10
  • Looking at your code there really should not be any reason that you are receiving a null reference exception. I think you are going to have to put more of your code out there in order to help troubleshoot. – kmacdonald Nov 28 '14 at 07:03
  • Post NRE stack trace, please. – Dennis Nov 28 '14 at 07:03

1 Answers1

1

Do not use MTObservableCollection.. Instead use BindingOperations.EnableCollectionSynchronization.

dev hedgehog
  • 8,698
  • 3
  • 28
  • 55