C# FAQ: What does Object reference not set to an instance of an object mean
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:FAQs |
| edit |
What does "Object reference not set to an instance of an object" mean?
Almost every day, beginning C# programmers ask:
- Why do I get the error "Object reference not set to an instance of an object"?
The reason is that the program is trying to access a member of a reference type variable which is set to null.
A simple example
The situation which causes this error message may not be so obvious as in the this simple example: The variable being referenced could be a class field, a parameter, or a local variable which had been instantiated but was later set to null. Nevertheless, here is a simple source code example that illustrates the problem:
using System; class Problem { static void Main() { Problem problem = null; // problem has not been instantiated // ... } public int ProblemProperty { get { return 0; } } public string ProblemMethod() { return null; } }
In the Main method, notice that problem is set to null. Therefore, the following code will throw a NullReferenceException:
try { // problem is null, and ProblemProperty cannot be called // on a null reference int prop = problem.ProblemProperty; } catch (NullReferenceException nre) { Console.WriteLine( "Cannot read ProblemProperty, problem is null.\n" + nre.Message); }
Since problem is null, it has no members to be accessed. Whereas the preceding example used a property call, the following example source code uses a method call:
try { // problem is null, and ProblemMethod cannot be called // on a null reference problem.ProblemMethod(); } catch (NullReferenceException nre) { Console.WriteLine( "Cannot call ProblemMethod(), problem is null.\n" + nre.Message); }
The preceding code will throw a NullReferenceException; because, problem is still null. It is irrelevant that the first example call was a property and the second was a method: they are both members of the type.
To solve this problem, be sure the problem object is instantiated before being referenced. The following sample source code instantiates problem and, so, avoids the error message:
Problem problem = new Problem(); // instantiate problem int prop = problem.ProblemProperty; // making member access possible
Now, problem contains a reference to an instance of Problem; so, any of its members can be called.
A more complex example
Many null reference problems are more difficult to diagnose and solve than this simple example. Sometimes there are multiple levels of indirection, making diagnosis more difficult.
In this example, assume problem references an object, but there is still an error in the following sample source code:
try { // still causes error because ProblemMethod() // returns null, Trim() cannot be called on null. problem.ProblemMethod().Trim(); } catch (NullReferenceException nre) { Console.WriteLine( "Cannot call Trim(), ProblemMethod() returns null.\n" + nre.Message); }
An error occurs in the preceding code, because ProblemMethod returns null. Accordingly, the code is trying to call the Trim method on a string reference that is set to null.
The best way to fix such a problem is to debug ProblemMethod to determine the reason null is returned.
In the preceding example, ProblemMethod should not return null. If null were a valid return value for ProblemMethod, then it would be necessary to check the ProblemMethod return value before calling any members of the return value type. For example:
if (problem.ProblemMethod() != null) { // return value is non-null problem.ProblemMethod().Trim(); }