System.NullReferenceException Class

public class NullReferenceException : SystemException

Base Types

Object
  Exception
    SystemException
      NullReferenceException

Assembly

mscorlib

Library

BCL

Summary

Represents the error that occurs when there is an attempt to dereference a null object reference.

Description

[Note: Applications throw the ArgumentNullException rather than NullReferenceException .

The following CIL instructions throw NullReferenceException :

]

Example

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().
 

See Also

System Namespace

Members

NullReferenceException Constructors

NullReferenceException() Constructor
NullReferenceException(System.String) Constructor
NullReferenceException(System.String, System.Exception) Constructor


NullReferenceException() Constructor

public NullReferenceException();

Summary

Constructs and initializes a new instance of the NullReferenceException class.

Description

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 .

See Also

System.NullReferenceException Class, System Namespace

NullReferenceException(System.String) Constructor

public NullReferenceException(string message);

Summary

Constructs and initializes a new instance of the NullReferenceException class.

Parameters

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.

Description

This constructor initializes the System.NullReferenceException.Message property of the new instance using message. If message is null , 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 to null .

See Also

System.NullReferenceException Class, System Namespace

NullReferenceException(System.String, System.Exception) Constructor

public NullReferenceException(string message, Exception innerException);

Summary

Constructs and initializes a new instance of the NullReferenceException class.

Parameters

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 .

Description

This constructor initializes the System.NullReferenceException.Message property of the new instance using message, and the System.NullReferenceException.InnerException property using innerException. If message is null , 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 .]

See Also

System.NullReferenceException Class, System Namespace