title: Can you capture the name of the object that throws a NullReferenceException?
body: <p>Is there a way to find out what <em>specific</em> object caused a <code>NullReferenceException</code>? I've read the page about <a href="https://stackoverflow.com/a/4660186/4416750">troubleshooting <code>NullReferenceException</code>s</a> and it talks about inspecting variables in the debugger and looking at the exception message. </p>
  
  <p>What if the exception was thrown in production code so you can't run a debugger to inspect the variables? The exception message shows the stack trace so you can see what method the exception was thrown in, but it does not say which specific object was <code>null</code>.</p>
  
  <p>I'd like to be able to add the name of the object that was <code>null</code> to the error message so that when I'm looking into reports from users and I come across a <code>NullReferenceException</code>, I can easily see what object was <code>null</code> and fix it. Does anyone know of a way to do this?</p>
  
  <p>I also found <a href="https://stackoverflow.com/questions/6980061/is-there-any-way-to-find-out-which-object-caused-the-nullreferenceexception">this question</a> which asked the same thing, but it was from 2011 and I don't know if anything has changed since then.</p>
  
  <p><strong>Edit</strong>: <a href="https://stackoverflow.com/questions/115573/detecting-what-the-target-object-is-when-nullreferenceexception-is-thrown">The question</a> that this is flagged as a duplicate is indeed a duplicate but is also very old (2008). Has anything changed since then?</p>
  
  <p><strong>Edit 2</strong>: I found <a href="https://github.com/dotnet/coreclr/issues/25#issuecomment-332675052" rel="nofollow noreferrer">this</a> when googling this question. Visual Studio can tell you what threw the <code>NullReferenceException</code>; is there any way to tap into this to add it to a log file?</p>