✅ Object Reference is null, even with try/catch

What dumb thing could I be missing? I'm trying to add a custom object to a simple dictionary of string + custom object. In the debug, you'll see that the _storedPoints is not null, and the modifiedRow is not null. While not shown, modifiedRow.Name is not null either. Could it be that this error is caught on another thread (since this is from a Parallel.ForEach), and I'm just not seeing the correct object in my debug?
No description
8 Replies
reflectronic
reflectronic2d ago
what is _storedPoints
engineertdog
engineertdogOP2d ago
No description
reflectronic
reflectronic2d ago
yes, this would be a problem you cannot modify a Dictionary from multiple threads at the same time an otherwise inexplicable NullReferenceException is exactly what i would expect to see if you did this
engineertdog
engineertdogOP2d ago
It only has issues on the first time running when I populate the SQLite database. Otherwise it's fine.
reflectronic
reflectronic2d ago
it does not change the fact that you are not allowed to do it the code in Dictionary assumes that you will not do it. if you violate these assumptions, it will create unexplainable errors at unpredictable times, like this one
engineertdog
engineertdogOP2d ago
I'm not sure why I'm still doing this anyway, apart from SQL performance. I need to query the SQLite database for each row rather than using memory to find these items.
reflectronic
reflectronic2d ago
ConcurrentDictionary Class (System.Collections.Concurrent)
Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.
engineertdog
engineertdogOP2d ago
Yeah, I could use that as a bypass until I get this reworked. This will work for small systems with less than 500k rows, but the systems that have 5 million rows is going to be problematic with this approach. Not sure how, but this can be marked closed.

Did you find this page helpful?