RE: NullReferenceException Message Feature Request

From: Bryan Livingston (anonymous_at_discussions.microsoft.com)
Date: 02/21/04

  • Next message: Daniel O'Connell [C# MVP]: "Re: Multiple put_ property accesors"
    Date: Sat, 21 Feb 2004 11:11:05 -0800
    
    

    For the record, here is what one of the program managers for the CLR had to say about it:

    The NullReferenceException occurs because an instruction like “call [eax+44]” or “mov edx, [esi+24]” has resulted in an access violation. We don’t retain nearly enough information to form a correspondence between a particular register being NULL at a particular EIP and the fact that a particular reference in the application was null. Especially since the EIP might be in a shared helper like a write barrier routine or an array helper. In those cases, we would have to perform a limited stack walk to get the effective EIP.

     

    The machinery that would be required to improve this error message is huge. For the foreseeable future, you will have to rely on debuggers, or on FX code explicitly checking and throwing an appropriate NullArgumentException.

    Chris.
    http://blogs.msdn.com/cbrumme

         
         ----- Bryan Livingston wrote: -----
         
         I'm guessing that NullReferenceExceptions are by far the most common exceptions thrown in .net.
         
         Why not add the type of the reference to the message? Have it say something like:
         Object reference of type 'string' not set to an instance of an object.
         
         This would really help when trying to figure out where the null reference is happening.
         
         I've just spent the last two days finding a null reference that happens very intermittently. Having the type would have saved me many hours.
         
         In addition to just the type, you might be able to use reflection to give a property name or variable name.
         
         Bryan Livingston
         Alphora.com


  • Next message: Daniel O'Connell [C# MVP]: "Re: Multiple put_ property accesors"

    Relevant Pages

    • Re: Cobol data protection? Get a dog...
      ... "Parameters specified in the USING phrase of a method Procedure Division ... some of you who are most knowledgable about the standards process. ... dereference a reference (back to the invokers WS or worse back up through the ... I am aware that you can pass an object reference to a method, ...
      (comp.lang.cobol)
    • RE: reference to handle / handle to reference
      ... As far as I know, there's no conversion between an object reference and an Int32, unless you pin objects using GCHandles and get their associated IntPtrs. ...
      (microsoft.public.dotnet.general)
    • Re: Error adding a reference to a developed serviced component
      ... should add a reference to the .NET assembly, ... The CLR ... will handle hooking up to the COM+ subsystem (which it knows it should do ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Q: closing forms
      ... The only real difference between passing by reference and by value takes ... passing the object reference by reference ... If a Is Nothing Then MsgBox("After call using ByRef, ... Private Sub MyProcByReference ...
      (microsoft.public.dotnet.languages.vb)
    • [JavaIDL] Releasing CORBA object references in Java ?
      ... I was thinking that reference counting was the job of GC. ... But here in Java, the introduction of _releaseand _duplicate() is ... Returns a duplicate of this CORBA object reference. ...
      (comp.lang.java.corba)