✅ 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?
8 Replies
what is
_storedPoints

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
It only has issues on the first time running when I populate the SQLite database. Otherwise it's fine.
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
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.
you can use a class like https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2?view=net-9.0 instead
ConcurrentDictionary Class (System.Collections.Concurrent)
Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.
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.