Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

There have been a number of similar questions here, but none explains what is happening in my case; so here goes.

I have the following (simplified) piece of code:

' row is a System.Data.DataRow
' _typeProperties is a Dictionary(Of String, PropertyInfo)

Dim data As New Dictionary(Of String, Object)
Dim elem As KeyValuePair(Of String, PropertyInfo)
Dim value As Object = Nothing
Try
    For Each elem In _typeProperties
        value = row.Item(elem.Key)
        data.Add(elem.Key, value)       ' NullReferenceException here
    Next
Catch ex As Exception When MyExceptionFilter(ex, data, elem, value)
End Try

Sometimes I get a NullReferenceException on the indicated line. This exception is exceedingly rare and I cannot reproduce it at will. However, I can modify my application, send it out to a customer and sure enough, after some days it will reproduce on its own.

The call stack is not very helpful:

StackTrace: 
  XXX.RowToType(DataRow row) in C:\XXX.vb:line 645.

Moreover, as you can see I included an exception filter in the Catch block. In there I write a minidump (with the call stack of the exception intact). Here is the relevant portion of the call stack revealed by the minidump:

  ...
  App.exe!MyExceptionFilter( ex,  data,  elem,  value) Line 627
  App.exe!RowToType( row) Line 647 + 0x1f bytes
  [External Code]   
  App.exe!RowToType(System.Data.DataRow row) Line 645 + 0x112 bytes
  App.exe!SomeClass.get_Item(Integer index) Line 1141 + 0xe bytes   
  user32.dll!_InternalCallWinProc@20()  + 0x23 bytes    
  user32.dll!_UserCallWinProcCheckWow@32()  + 0x693 bytes   
  ...

The exception happens somewhere in the [External Code] block; then the filter of the Catch block gets executed (line 2 & line 1).

At the moment of the exception, these are the values for the three involved variables:

  data: Not Nothing; 
  elem: Not Nothing; 
  elem.Value: Not Nothing (Int32 ID)
  elem.Key: Not Nothing
  value: Nothing

So there appears to be absolutely no reason for data.Add to throw a NullReferenceException.

As some people suggested in other questions, there might be some threading issue. However, the dictionary to which I am writing can by definition only be visible to one thread. (And to be sure, I also checked the minidump to make sure that no thread is executing the same code.)

I might just silently ignore this exception, but I'd rather figure this out.

share|improve this question
any multi-threading issues regarding _typeProperties? – JeffRSon 17 hours ago
@JeffRSon: By design there should be none (since the containing object is only known to the UI thread). Moreover, I checked the Minidump to be absolutely sure no other thread was even near that code (meaning no other thread had even an instance of the class the exception happened in). – Andreas 17 hours ago
Can you reproduce it on your machine "after a few days"? Or has it ever only happened on that customers machine? – Daniel Hilgarth 17 hours ago
It has only ever happened on one particular machine at the customer. Although that bit of code is also executed fairly often, I have never been able to reproduce it while debugging. I did not try to reproduce it on my machine without debugger or with debugger in release mode (no time). – Andreas 17 hours ago

1 Answer

value: Nothing would be a valid reason to get a Null Reference exception. It's possible that the string in your DataRow is Nothing (if it the column didn't exist, you'd get an exception).

share|improve this answer
So why would that cause an exception in data.Add? – Andreas 17 hours ago
Read slightly wrong. It it possible another thread is editing _typeProperties? – xen-0 17 hours ago
no; see my comment above – Andreas 17 hours ago
Just because another thread isn't in the same block of code, doesn't mean one of the objects used isn't being touched - you didn't indicate the latter. The concern isn't the object you're writing to, but those that you're reading from. – xen-0 17 hours ago
I meant the latter; there is no other thread with access to the objects this thread is touching – Andreas 17 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.