The exception that is thrown when there is an attempt to dereference a null object reference.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
|NullReferenceException()||Initializes a new instance of the class, setting the 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.|
|NullReferenceException(String)||Initializes a new instance of the class with a specified error message.|
|NullReferenceException(SerializationInfo, StreamingContext)||Initializes a new instance of the class with serialized data.|
|NullReferenceException(String, Exception)||Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception.|
|Data||Gets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)|
|HelpLink||Gets or sets a link to the help file associated with this exception. (Inherited from Exception.)|
|HResult||Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)|
|InnerException||Gets the Exception instance that caused the current exception. (Inherited from Exception.)|
|Message||Gets a message that describes the current exception. (Inherited from Exception.)|
|Source||Gets or sets the name of the application or the object that causes the error. (Inherited from Exception.)|
|StackTrace||Gets a string representation of the immediate frames on the call stack. (Inherited from Exception.)|
|TargetSite||Gets the method that throws the current exception. (Inherited from Exception.)|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetBaseException||When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetObjectData||When overridden in a derived class, sets the SerializationInfo with information about the exception. (Inherited from Exception.)|
|GetType||Gets the runtime type of the current instance. (Inherited from Exception.)|
In XNA Framework 3.0, this member is inherited from Object::GetType().
In Portable Class Library Portable Class Library, this member is inherited from Object::GetType().
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ToString||Creates and returns a string representation of the current exception. (Inherited from Exception.)|
A exception is thrown when you try to access a member on a type whose value is nullptr. A exception typically reflects developer error and is thrown in the following scenarios:
You've forgotten to instantiate a reference type. In the following example, names is declared but never instantiated:
Some compilers issue a warning when they compile this code. Others issue an error, and the compilation fails. To address this problem, instantiate the object so that its value is no longer nullptr. The following example does this by calling a type's class constructor.
You get a null return value from a method, and then call a method on the returned type. This sometimes is the result of a documentation error; the documentation fails to note that a method call can return nullptr. In other cases, your code erroneously assumes that the method will always return a non-null value.
The code in the following example assumes that the Array::Find<T> method always returns Person object whose FirstName field matches a search string. Because there is no match, the runtime throws a exception.
To address this problem, test the method's return value to ensure that it is not nullptr before calling any of its members, as the following example does.
A exception is thrown by a method that is passed nullptr. Some methods validate the arguments that are passed to them. If they do and one of the arguments is nullptr, the method throws an System::ArgumentNullException exception. Otherwise, it throws a exception. The following example illustrates this scenario.
To address this issue, make sure that the argument passed to the method is not nullptr, or handle the thrown exception in a try…catch…finally block. For more information, see Handling and Throwing Exceptions.
The following Microsoft intermediate language (MSIL) instructions throw : callvirt, cpblk, cpobj, initblk, ldelem.<type>, ldelema, ldfld, ldflda, ldind.<type>, ldlen, stelem.<type>, stfld, stind.<type>, throw, and unbox.
uses the HRESULT COR_E_NULLREFERENCE, which has the value 0x80004003.
For a list of initial property values for an instance of , see the NullReferenceException constructors.
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
XNA FrameworkSupported in: 3.0, 2.0, 1.0
Portable Class LibrarySupported in: Portable Class Library
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8