BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.SystemException System.Runtime.InteropServices.ComVisible(true)

The following example demonstrates an error that causes a 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().`

A exception is thrown when you try to access a member on a type whose value is null. The following example illustrates one such scenario:

code reference: System.NullReferenceException.Class#1

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 null. The following example does this by calling a type's class constructor.

code reference: System.NullReferenceException.Class#2

More commonly, a exception is thrown by a method that is passed null. Some methods validate the arguments that are passed to them. If they do and one of the arguments is null, the method throws an exception. Otherwise, it throws a exception. The following example illustrates this scenario.

code reference: System.NullReferenceException.Class#3

To address this issue, make sure that the argument passed to the method is not null, 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 constructors.

The exception that is thrown when there is an attempt to dereference a null object reference.

- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  The following table shows the initial property values for an instance of .

  Property

  Value

  null.

  The localized error message string.

  Initializes a new instance of the class, setting the 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.

  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  The following table shows the initial property values for an instance of .

  Property

  Value

  null.

  The error message string.

  Initializes a new instance of the class with a specified error message.

  A that describes the error. The content of 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.
  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  This constructor is called during deserialization to reconstitute the exception object transmitted over a stream. For more information, see [<topic://cpconSerialization>].

  Initializes a new instance of the class with serialized data.

  The object that holds the serialized object data.
  The contextual information about the source or destination.
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  An exception that is thrown as a direct result of a previous exception should include a reference to the previous exception in the property. The property returns the same value that is passed into the constructor, or null if the property does not supply the inner exception value to the constructor.

  The following table shows the initial property values for an instance of .

  Property

  Value

  The inner exception reference.

  The error message string.

  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.

  The error message that explains the reason for the exception.
  The exception that is the cause of the current exception. If the parameter is not null, the current exception is raised in a catch block that handles the inner exception.
  0
0