public class NullReferenceException : SystemException
Object
Exception
SystemException
NullReferenceException
mscorlib
BCL
Represents the error that occurs when there is an attempt to dereference a null object reference.
[Note: Applications throw the ArgumentNullException rather than NullReferenceException .The following IL instructions throw NullReferenceException :
]
- callvirt
- cpblk
- cpobj
- ldelema
- ldelem.<type>
- ldfld
- ldflda
- ldind.<type>
- ldlen
- stelem.<type>
- stind.<type>
- stfld
- throw
- unbox
- initblk
The following example demonstrates an error that causes a NullReferenceException exception.
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
Caught error: System.NullReferenceException: Object reference not set to an instance of an object. at NullRefExample.Main().
System Namespace
NullReferenceException Constructors
NullReferenceException() Constructor
NullReferenceException(System.String) Constructor
NullReferenceException(System.String, System.Exception) Constructor
public NullReferenceException();
Constructs and initializes a new instance of the NullReferenceException class.
This constructor initializes the System.NullReferenceException.Message property of the new instance to a system-supplied message that describes the error, such as "The value 'null' was found where an instance of an object was required." This message takes into account the current system culture.The System.NullReferenceException.InnerException property is initialized to
null
.
System.NullReferenceException Class, System Namespace
public NullReferenceException(string message);
Constructs and initializes a new instance of the NullReferenceException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
This constructor initializes the System.NullReferenceException.Message property of the new instance using message. If message isnull
, the System.NullReferenceException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.NullReferenceException.InnerException property is initialized tonull
.
System.NullReferenceException Class, System Namespace
public NullReferenceException(string message, Exception innerException);
Constructs and initializes a new instance of the NullReferenceException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- innerException
- An instance of Exception that is the cause of the current Exception. If innerException is non-null, then the current Exception was raised in a catch block handling innerException .
This constructor initializes the System.NullReferenceException.Message property of the new instance using message, and the System.NullReferenceException.InnerException property using innerException. If message isnull
, the System.NullReferenceException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.[Note: For information on inner exceptions, see System.Exception.InnerException .]
System.NullReferenceException Class, System Namespace