views:

57

answers:

1

Ever since I started programming this question has been annoying me, not "where is my code null" but specifically is there any way to get the type of the object that is null from the exception?

Failing that, can anyone provide a blog post or msdn article that explains the reason why the .NET Framework doesn't (or can't) provide these details?

+1  A: 

Um... Because it's null.

In C#, a reference type is a pointer to something. A null pointer isn't pointing to anything. You are asking, "What type of thing would this point to if it was pointing to something". That's sort of like getting a blank sheet of paper, and asking, "What would this say if it had something written on it?"

UPDATE: If the framework can't know the type of a null pointer, can't it know what type it's supposed to be? Well, it might. Then again, it might not. Consider:

 MyClass myobj = null;
 int hash = myobj.GetHashCode();

Unless you overrode it in MyClass, GetHashCode is defined in System.Object. Should you get a complaint that myobj needs to be a System.Object? Now, when we check ourselves, we're completely free to specify the required type.

  SomeFunc(MyClass myparam)
  {
       if (myparam == null)
           throw new ArgumentNullException("myparam must be a MyClass object");
  }

But now we are talking about application code, not CLR code. Which makes you "real" question "Why don't people write more informative exception messages?", which is, as we say here at SO "subjective and argumentative"

So, you basically want the system level exception to know the type information which is only known at the applications level, which we'd need so way to communicate. Something like:

  SomeFunc(MyClass myparam)
  {
       if (myparam == null)
           throw new ArgumentNullException("myparam", typeof(MyClass));
  }

But that's not really buying us much, and if you really want it, you could write it yourself:

  public class MyArgumentNullException : ArgumentNullException
  {
      public MyArgumentNullException(string name, Type type) 
          :base(name + "must be a " + type.Name + " object");
  }
James Curran
I love that analogy.
Tim S. Van Haren
but I would think that C# at least knows what type it was expecting to be there. Imagine I am calling a method that accepts a String and a FileInfo, one of them is null. Cannot the framework at least remember which parameter it was looking for and go from there. Similarity if I do: _fInfo.ToString() and myobject is null it should be able to tell me that _fInfo was expected to be of type FileInfo. I can understand if all the variables were declared as "var" but I declared it as FileInfo above so would think that it should really map between them and go "oh I'm null, I was expecting a FileInfo"
Seph
Declaring something as `var` is irrelevant here. The compiler always knows the type. The problem is ArgumentNullException doesn't.
James Curran