Object reference not set to an instance of object

An Object is an instance of a Class , it is stored some where in memory. A reference is what is used to lớn describe the pointer to the memory location where the Object resides. The message "object reference not mix lớn an instance of an object" means that you are referring lớn an object the does not exist or was deleted or cleaned up. This would usually be a run-time error .

Bạn đang xem: Object reference not set to an instance of object

*

Reference Types

Variables in .NET are either reference types or value types . Value types are primitives such as integer & boolean or structure. For ex. Boolean variables, when declared, have a mặc định value:

bool isTrue;//isTrue == false

Reference types, when declared, vày not have a mặc định value:

class MyClass MyClass mClass; //default value is null

If you try lớn access a thành viên of a class (here MyClass) then you get a System.NullReferenceException. Which is the same as "object reference not set to an instance of an object" . It indicates that you are trying lớn access thành viên fields, or function types, on an object reference that points khổng lồ null. That means the reference to lớn an Object which is not initialized. However, the null-reference exception is designed as a valid runtime condition that can be thrown and caught in normal program flow.

Handling NullReferenceException

It"s usually better lớn avoid a NullReferenceException than khổng lồ handle it after it occurs. To prsự kiện the error, objects that could be null should be tested for null before being used.

Xem thêm: Top 9 Khuôn Vẽ Lông Mày Ngang Giá Cạnh Tranh, Với Nhiều Ưu, Bộ 3 Khuôn Vẽ Chân Mày Giá Tốt Tháng 3,, 2021

if (mClass != null) // Go ahead and use mClass mClass.property = ...else // Whoops! mClass is null và cannot be used without first assigning it to lớn an instance reference // Attempting to use mClass here will result in NullReferenceException

Object Reference variable is just lượt thích "pointer in C" but not exactly a pointer. A NullReferenceException typically reflects developer error and is thrown in the following scenarios:

Forgotten lớn instantiate a reference type.

Xem thêm: Top 40 Đề Thi Tin Học Lớp 6 Học Kì 1, Đề Thi Học Kì 1 Lớp 6 Môn Tin Học

Forgotten khổng lồ dimension an array before initializing it. Is thrown by a method that is passed null. Get a null return value from a method, then gọi a method on the returned type. Using an expression to retrieve sầu a value và, although checking whether the value is null. Enumerating the elements of an array that contains reference types, and attempt khổng lồ process one of the elements.
*

Handling an exception can make your code harder lớn maintain and understvà, & can sometimes introduce other bugs. However, there are many situations where handling the error can be useful:

Your application can ignore objects that are null. You can restore the state of your application khổng lồ a valid state. You want lớn report the exception.

C#8.0 Nullable reference typesC#8.0 introduces nullable reference types và non-nullable reference types. So only nullable reference types must be checked to lớn avoid a NullReferenceException. Since this is a breaking change, it is launched as an opt-in feature.


Chuyên mục: Blogs