See Also: NullReferenceException Members
Applications throw the ArgumentNullException rather than NullReferenceException .
The following CIL instructions throw NullReferenceException :
The following example demonstrates an error that causes a NullReferenceException exception.
C# Example
using System;
public class Ints {
public int[] myInts;
}
public class NullRefExample {
public static void Main() {
Ints ints = new Ints();
try {
int i = ints.myInts[0];
}
catch( NullReferenceException e ) {
Console.WriteLine( "Caught error: {0}.", e);
}
}
}
The output is
Example
Caught error: System.NullReferenceException: Object reference not set to an instance of an object. at NullRefExample.Main().