0

Why aren't .net error messages more helpful?

For example:

System.NullReferenceException: Object reference not set to an instance of an object.

Wouldn't it be possible through reflection to get the null object's parent so that the error could tell which object in the following access chain are the culprit of the error so I don't have to fire up the debugger:

Object1.Object2.Object3.Object4
sarnold
  • 102,305
  • 22
  • 181
  • 238
terjetyl
  • 9,497
  • 4
  • 54
  • 72
  • 7
    isn't that what the stack trace is for? – Nick Rolando Oct 07 '11 at 23:31
  • 4
    The cost of such a feature most likely outweighed the benefits. – ChaosPandion Oct 07 '11 at 23:32
  • 2
    http://stackoverflow.com/questions/4626418/why-dont-object-reference-error-exceptions-in-net-tell-me-which-object-was-null – Haris Hasan Oct 07 '11 at 23:35
  • I would say the stack trace tells you in what line the null reference occurred but not which object in the chain is null – terjetyl Oct 07 '11 at 23:45
  • So you set a breakpoint in the debugger and you find out. It makes no sense to go to such lengths in the .NET Framework to make it easier for a programmer to find a dumb error. The better technique is to allow a tool like ReSharper to tell you that you are trying to reference a value that might be null. – John Saunders Oct 08 '11 at 00:21
  • 3
    Your idea that there is a "null object" is fallacious. A null reference is *the absense of an object*. **Information about what variable that null reference originally came from is not preserved by the runtime engine**. No additional information is reported because the runtime does not record that information anywhere. – Eric Lippert Oct 08 '11 at 04:10

1 Answers1

4

The stack trace would get you most of the way there? Unless you have massive methods.

JBB
  • 657
  • 5
  • 14