Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object reference not set to an instance of an object.Why doesn't .NET show which object is `null`?

Tags:

c#

.net

People also ask

How do I fix object reference is not set to an instance of an object?

To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.

How do I fix null reference exception in C#?

You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does. For more information on declaring and initializing arrays, see Arrays and Arrays. You get a null return value from a method, and then call a method on the returned type.

What is object reference not set to an instance of an object in C#?

So, this error description says that an object that is being called to get or set its value has no reference. This means that you are trying to access an object that was not instantiated.


(For information about the new exception helper in Visual Studio 2017 see the end of this answer)


Consider this code:

String s = null;
Console.WriteLine(s.Length);

This will throw a NullReferenceException in the second line and you want to know why .NET doesn't tell you that it was s that was null when the exception was thrown.

To understand why you don't get that piece of information you should remember that it is not C# source that executes but rather IL:

IL_0001:  ldnull      
IL_0002:  stloc.0     // s
IL_0003:  ldloc.0     // s
IL_0004:  callvirt    System.String.get_Length
IL_0009:  call        System.Console.WriteLine

It is the callvirt opcode that throws the NullReferenceException and it does that when the first argument on the evaluation stack is a null reference (the one that was loaded using ldloc.0).

If .NET should be able to tell that it was s that was a null reference it should in some way track that the first argument on the evaluation stack originated form s. In this case it is easy for us to see that it is s that was null but what if the value was a return value from another function call and not stored in any variable? Anyway, this kind of information is not what you want to keep track of in a virtual machine like the .NET virtual machine.


To avoid this problem I suggest that you perform argument null checking in all public method calls (unless of course you allow the null reference):

public void Foo(String s) {
  if (s == null)
    throw new ArgumentNullException("s");
  Console.WriteLine(s.Length);
}

If null is passed to the method you get an exception that precisely describes what the problem is (that s is null).


Four years later Visual Studio 2017 now has a new exception helper that will try to tell what is null when a NullReferenceException is thrown. It is even able to give you the required information when it is the return value of a method that is null:

Visual Studio 2017 exception helper

Note that this only works in a DEBUG build.


How do you want the error message in the following case look like?

AnyObject.GetANullObject().ToString();

private object GetANullObject()
{
  return null;
}

No variable names to report here!


Well, that's upto engineers at Microsoft to answer. But you can obviously use a debugger and add watch to find out which of those has a problem.

However, the exception is NullReferenceException which means the reference does not exist . You can't get the object which hasn't been created at all.

but why .NET don't tell us which object is null? Because it does not know which object is null. The object simply does not exist!

Same is the case when I say, C# is compiled to .NET IL code. The .NET IL code does not know the names or expressions. It only knows references and their location. Here too, you cannot get what does not exist. The expression or the variable name does not exist.

Philosophy: You cannot make an omlette if you don't have an egg in the first place.


Not sure, but this may be because .Net doesn't knows whether it is predefined class or user-defined. If it is predefined then it can be null(like string which occupies 2 Bytes) but if it is user-defined than we have to create an instance of it so that it knows that this object will occupy this much memory. So therefore it throws error at run-time.