Detecting what the target object is when NullReferenceException is thrown

后端 未结 10 805
时光说笑
时光说笑 2020-12-03 01:18

I\'m sure we all have received the wonderfully vague \"Object reference not set to instance of an Object\" exception at some time or another. Identifying the object that is

相关标签:
10条回答
  • 2020-12-03 01:45

    If you're catching your exceptions for friendly user messages or logging you'd probably want the debugger to stop at an exception while debugging. Go to Debug/Exceptions and check the exception types you want the debugger to stop running at, System.NullReferenceException in your case.

    0 讨论(0)
  • 2020-12-03 01:46

    Set VS to break on exceptions, then when you get your error it's usually pretty obvious what line it's on. The stack trace window will tell you how you got there. Not much else you can do apart from that.

    0 讨论(0)
  • 2020-12-03 01:48

    you can check the Message and InnerException properties

    http://msdn.microsoft.com/en-us/library/system.exception.innerexception.aspx

    0 讨论(0)
  • 2020-12-03 01:52

    There's really not much you can do besides look at the stack trace; if you're dereferencing multiple object references in the same line of code, there's no way to determine which one is null without setting a breakpoint. You could avoid this by only dereferencing one object per line, but that would result in some pretty terrible-looking code.

    0 讨论(0)
提交回复
热议问题