NullReferenceException on Object Action

NullReferenceException is thrown on a string on which all involved objects are valid. StackTrace shows that line # is 432.

Code

enter image description here

Here Flags and tempFlags are both datatables. The column data types of both data types are primitive (decimal, datetime, short). An application is a multi-threaded application, and a piece of code belongs to a stream function. Flags decalified at the instance level, that is, shared across all threads, while tempFlags declared inside the thread function.

Here in this particular instance of time, Flags contains 1946 entries and tempFlags contains 1. So why is this a NullReferenceEx exception

Edit # 1

 ex.InnerException null ex.StackTrace at System.Data.DataTable.RecordStateChanged(Int32 record1, DataViewRowState oldState1, DataViewRowState newState1, Int32 record2, DataViewRowState oldState2, DataViewRowState newState2) at System.Data.DataTable.InsertRow(DataRow row, Int64 proposedID) at System.Data.DataTable.MergeRow(DataRow row, DataRow targetRow, Boolean preserveChanges, Index idxSearch) at System.Data.Merger.MergeTable(DataTable src, DataTable dst) at System.Data.Merger.MergeTableData(DataTable src) at System.Data.Merger.MergeTable(DataTable src) at System.Data.DataTable.Merge(DataTable table, Boolean preserveChanges, MissingSchemaAction missingSchemaAction) at System.Data.DataTable.Merge(DataTable table) at [...].cs:line 432" ex.Data {System.Collections.ListDictionaryInternal} [System.Collections.ListDictionaryInternal]: {System.Collections.ListDictionaryInternal} IsFixedSize: false IsReadOnly: false Keys: {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} Values: {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} ex.Message "Object reference not set to an instance of an object." ex.Source "System.Data" 

Edit # 2

It seems that the Merge statement is not thread safe, because after putting line 432 inside the lock, the exception has passed, SO FAR.

+6
source share
3 answers

An explanation of this exception (IMO) specific to this piece of code.

Lets us say that thread A executes Merge and passes datatable Dt1 , which is stored in Merge as referenceToDatatable , meanwhile

Stream B enters and passes Dt2 to Merge , which is stored in Merge as referenceToDatatable (the link is the same since non-primitive objects are passed by reference, Merge is not thread safe and there is no Lock) and, therefore, Dt1 overwritten by Dt2 .

So far, the exception does not exist, since Dt2 has the same structure and is not null .

Now thread B is paused and thread A enters, terminates Merge and shuts down, therefore invalidates Dt1 , which also resets referenceToDatatable .

Now thread B comes in and finds referenceToDatatable = null Exception

+1
source

Since this happens when inserting a new row, System.Data.DataTable.InsertRow(DataRow row, Int64 proposedID) , I would suggest that there is a restriction indicating that the field cannot be null. And you are trying to insert zero from the source table.

Or, there is a calculated column, and one of the input columns is null.

+2
source

Whenever you get a NullReferenceException from within the framework and you are multithreaded, it is almost certainly a thread safety issue when you do not apply locks where you should be.

+2
source

All Articles