I have some code and when it executes, it throws a NullReferenceException
, saying:
Object reference not set to an instance of an object.
What does this mean, and what can I do to fix this error?
I have some code and when it executes, it throws a NullReferenceException
, saying:
Object reference not set to an instance of an object.
What does this mean, and what can I do to fix this error?
While what causes a NullReferenceExceptions and approaches to avoid/fix such an exception have been addressed in other answers, what many programmers haven't learned yet is how to independently debug such exceptions during development.
In Visual Studio this is usually easy thanks to the Visual Studio Debugger.
First, make sure that the correct error is going to be caught - see How do I allow breaking on 'System.NullReferenceException' in VS2010? Note1
Then either Start with Debugging (F5) or Attach [the VS Debugger] to Running Process. On occasion it may be useful to use
Debugger.Break
, which will prompt to launch the debugger.Now, when the NullReferenceException is thrown (or unhandled) the debugger will stop (remember the rule set above?) on the line on which the exception occurred. Sometimes the error will be easy to spot.
For instance, in the following line the only code that can cause the exception is if
myString
evaluates to null. This can be verified by looking at the Watch Window or running expressions in the Immediate Window.In more advanced cases, such as the following, you'll need to use one of the techniques above (Watch or Immediate Windows) to inspect the expressions to determine if
str1
was null or ifstr2
was null.Once where the exception is throw has been located, it's usually trivial to reason backwards to find out where the null value was [incorrectly] introduced --
Take the time required to understand the cause of the exception. Inspect for null expressions. Inspect the previous expressions which could have resulted in such null expressions. Add breakpoints and step through the program as appropriate. Use the debugger.
1 If Break on Throws is too aggressive and the debugger stops on an NPE in the .NET or 3rd-party library, Break on User-Unhandled can be used to limit the exceptions caught. Additionally, VS2012 introduces Just My Code which I recommend enabling as well.
A
NullReferenceException
is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.For example:
When a string method of an empty string accessed:
When a property of a null object accessed:
Another general case where one might receive this exception involves mocking classes during unit testing. Regardless of the mocking framework being used, you must ensure that all appropriate levels of the class hierarchy are properly mocked. In particular, all properties of
HttpContext
which are referenced by the code under test must be mocked.See "NullReferenceException thrown when testing custom AuthorizationAttribute" for a somewhat verbose example.
There is a scenario that can happen that is Class related. The question ended up getting closed prior to my stating the resolution: https://stackoverflow.com/questions/43348009/unable-to-instantiate-class
Beware of classes not instantiating: If any part of your constructor in a class throws a
null reference exception
the class does not instantiate. In my case it was trying to get a connection string from the web.config that that did not exist.I instantiated a class:
Inside the class itself was a call to get a connection string from the
web.config
. This part of the constructor threw an null value exception somyClass
was null.If you ever have a situation where a class in not instantiating, try making sure that no part of the class constructor Is throwing a
null value exception
. F-11 and step through the class and make sure there are no nulls.If one is getting this message during saving or compiling the build, just close all the files and then open any file to compile and save.
For me the reason was that I had rename the file and old file was still open.
If we consider common scenarios where this exception can be thrown, accessing properties withing object at the top.
Ex:
in here , if address is null , then you will get NullReferenceException.
So, as a practice we should always use null check, before accessing properties in such objects (specially in generic)