Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. NullReferenceException Class
DefinitionThe exception that is thrown when there is an attempt to dereference a null object reference. In this article
InheritanceInheritanceAttributes
RemarksA NullReferenceException exception is thrown when you try to access a member on a type whose value is
The following Microsoft intermediate language (MSIL) instructions throw
NullReferenceException: NullReferenceException uses the HRESULT COR_E_NULLREFERENCE, which has the value 0x80004003. For a list of initial property values for an instance of NullReferenceException, see the NullReferenceException constructors. Handling NullReferenceException in release code It's usually better to avoid a NullReferenceException than to handle it after it occurs. Handling an exception can make your code harder to maintain and understand, and can sometimes introduce other bugs. A NullReferenceException is often a non-recoverable error. In these cases, letting the exception stop the app might be the best alternative. However, there are many situations where handling the error can be useful:
ConstructorsProperties
Methods
Events
Applies toSee also
When an exception is thrown by a method that is executing under several layers of method calls?8) When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.
What happens if a program does not handle an unchecked exception?If your code does not handle and exception when it is thrown, this prints an error message and crashes the program.
When you write a method that throws a checked exception you must?If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using the throws keyword. In checked exception, there are two types: fully checked and partially checked exceptions.
When the code in a try block may throw more than one type of exception you need to write a catch clause for each type of exception that could potentially be thrown?When the code in a try block may throw more than one type of exception, you need to write a catch clause for each type of exception that could potentially be thrown. All of the exceptions that you will handle are instances of classes that extend this class.
|