9

I have a problem with a uge solution at work that gets a lot of "Object reference not set to an instance of an object" errors. What's the best way to determine the null object(s) causing the exception?

I can try catch all those exceptions in one place, but can't find a way to determine the member that is null so I can fix it properly.

try {
}
catch (Exception ex)
{
if (ex is ReferenceNullException)
ex.??
}
}

Since I can view the stacktrace it would be reasonable to think you could also get what caused the error.

Bruno Silva
  • 167
  • 1
  • 2
  • 10
  • trial and error? `if (thisThingWhichCouldBeNull == null); else if (thisOtherThing == null); else if (etcetera); ...` At least if you need to programmatically determine which object was null at runtime. – Wug Aug 27 '12 at 15:08
  • 9
    Why not just let the debugger stop on exceptions? then you see where your error is... – sloth Aug 27 '12 at 15:08
  • 3
    I assume you mean the variable or expression that was `null`. Asking for the object that was `null` is nonsensical. – CodesInChaos Aug 27 '12 at 15:09
  • 1
    Enable "Break on first chance exceptions" and debug the code. Working code should not throw `NullReferenceException`s. – CodesInChaos Aug 27 '12 at 15:10
  • 1
    If you actually need this, you likely have methods that are way too big. Small, concise methods still can get NREs but they're much easier to diagnose. – Austin Salonen Aug 27 '12 at 15:11
  • Can't debug, it's production code. And I can't repo most of the bugs. – Bruno Silva Aug 27 '12 at 15:17
  • 1
    If you can't debug it, make sure the corresponding .PDB files are copied aside the .DLL files. The exception stack frame will then contain the source code line. This helps determine what *could* be null. – Simon Mourier Aug 27 '12 at 16:02
  • possible duplicate of [Is there any way to find out which object caused the null reference exception?](http://stackoverflow.com/questions/6980061/is-there-any-way-to-find-out-which-object-caused-the-null-reference-exception) – Jon Schneider Sep 22 '15 at 14:16

3 Answers3

12

Think about it for a second. It's a NullReferenceException. That means you're trying to call a method or access a property on a NULL REFERENCE to an object. That means the object reference you're trying to access is EMPTY, null. It does not exist.

So what you're trying to find actually does not exist.

Normally to track down which object reference is null a debugger is used. Just set a breakpoint on the line causing the exception and inspect all variables to see which one is null.

Debugger is your greatest tool.

Strelok
  • 50,229
  • 9
  • 102
  • 115
  • 6
    Started out a bit philosophical, but followed up with sound advice. +1. – KeithS Aug 27 '12 at 15:12
  • 11
    I know that. The problem is that this is a solution already in prodution and I can only get errors through email. Can't repo most of the errors some users get. – Bruno Silva Aug 27 '12 at 15:14
  • @BrunoSilva well I think at the very least you need to see a stack trace and pin point the line with the error. No other way about it. – Strelok Aug 27 '12 at 15:18
  • Ok, thought there could be an easier way since I can get almost any info from the executing method through reflection. – Bruno Silva Aug 27 '12 at 15:22
  • 2
    Reflection can do some cool things, but determining what just failed within the try block isn't a good use of reflection, regardless of possibility. Change the email-sending routine to send a stack trace, and consider pushing out PDB files with the app (so you get line numbers in those stack traces, instead of byte offsets) – KeithS Aug 27 '12 at 15:34
  • @BrunoSilva Exactly. In very large systems, it is often very difficult to reproduce the issue as it can be data dependent and if the data changes, you may lose the opportunity without a well timed DB backup. – Shiv Mar 02 '16 at 00:40
  • @KeithS should putting pdb's be of any use even if the project is published on release ? Would it still give us the line numbers ? – eran otzap May 05 '16 at 20:50
  • The null reference has a symbol name. Seems like it could be included in the exception output. "myDate is null" – Mark Lauter Dec 17 '18 at 19:42
3

If you are not able debug NullReferenceException with IDE in case that it only happens at customer side or it is difficult to reproduce, then NullReferenceException.StackTrace which has FUNCTION/FILE/LINE information will help you to locate the null object, NullReferenceException.ToString() also include StackTrace, For example:

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

at WindowsFormsApplication3.Form1.button1_Click(Object sender, EventArgs e) in D:\vcs\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs:line 26

To enable line number for release build, pls check this post Display lines number in Stack Trace for .NET assembly in Release mode

Community
  • 1
  • 1
Fengtao Ding
  • 706
  • 8
  • 17
-1

Check out the documentation on Try-Catch http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.71).aspx

You can have multiple catches in a try catch to handle different exceptions in their own way

try{
//do stuff
} Catch (NullReferenceException ex){

} Catch (Exception ex) {

}
James
  • 2,445
  • 2
  • 25
  • 35
  • 12
    You should virtually never catch (or throw) a null reference exception in production code. You should check for `null` first in an `if` block, if appropriate, to ensure you never get an NRE in the first place. You shouldn't use exceptions for control flow. – Servy Aug 27 '12 at 15:29