Object Reference not set to an Instance of an object là lỗi gì ?

Object Reference not set to an Instance of an object là lỗi gì ?

Ngôn ngữ lập trình C# đang là một trong những ngôn ngữ lập trình được rất nhiều các lập trình viên lựa chọn dùng để viết lên các phần mềm ứng dụng phổ biến hiện nay. Bản chất của ngôn ngữ lập trình này có nhiều ưu điểm hơn so với các ngôn ngữ khác như C, Visual Basic , Java , PHP ….

Bạn là một lập trình viên hay bạn đang là sinh viên đang tập làm quen với ngôn ngữ C# mà gặp lỗi Object Reference not set to an Instance of an object mà không hề biết Object Reference not set to an Instance of an object là lỗi gì ?

Thực ra đây là lỗi rất nhiều người lập trình gặp phải không hẳn chỉ là những bạn sinh viên mà có thể những bạn lập trình viên đang đi làm ở các doanh nghiệp cũng gặp phải mà không hề biết nó là lỗi gì và không biết cách xử lý như thế nào . Chính vì thế hôm nay daydore.com xin chia sẻ thêm về lỗi này nhé .

Object Reference not set to an Instance of an object là lỗi gì ?

Đây là một lỗi thường chúng ta gặp phải khi lập trình phần mềm ứng dụng trên ngôn ngữ C# , lỗi này thể hiện là chúng ta đang thiếu một giá trị tham chiếu bên trong code .

Đọc Thêm:  Nguyên nhân IDM không bắt link Google Drive và cách khắc phục

Cách khắc phục lỗi Object Reference not set to an Instance of an object :

Các biến trong .NET là loại tham chiếu hoặc loại giá trị. Các loại giá trị là các nguyên hàm như số nguyênbooleans hoặc cấu trúc (và có thể được xác định do chúng kế thừa từ System.ValueType). Các biến Boolean , khi được khai báo, có giá trị mặc định như sau :

bool mybool;
//mybool == false

Các loại tham chiếu, khi được khai báo, không có giá trị mặc định:

class ExampleClass
{
}

ExampleClass exampleClass; //== null

Nếu bạn cố gắng truy cập một thành viên của một thể hiện lớp bằng cách sử dụng tham chiếu null thì bạn sẽ nhận được System.NullReferenceException . Tương tự như Object reference not set to an instance of an object.

Đoạn mã sau đây giúp bạn hóa giải được thông báo lỗi trên :

static void Main(string[] args)
{
    var exampleClass = new ExampleClass();
    var returnedClass = exampleClass.ExampleMethod();
    returnedClass.AnotherExampleMethod(); //NullReferenceException here.
}

class ExampleClass
{
    public ReturnedClass ExampleMethod()
    {
        return null;
    }
}

class ReturnedClass
{
    public void AnotherExampleMethod()
    {
    }
}

Đây là một lỗi rất phổ biến và có thể xảy ra vì tất cả các loại lý do. Nguyên nhân gốc rễ thực sự phụ thuộc vào kịch bản cụ thể mà bạn gặp phải.

Nếu bạn đang sử dụng API hoặc gọi các phương thức có thể trả về null thì điều quan trọng là phải xử lý việc này một cách duyên dáng . Phương thức chính ở trên có thể được sửa đổi theo cách mà người dùng không bao giờ nhìn thấy NullReferenceException:

static void Main(string[] args)
{
    var exampleClass = new ExampleClass();
    var returnedClass = exampleClass.ExampleMethod();

    if (returnedClass == null)
    {
        //throw a meaningful exception or give some useful feedback to the user!
        return;
    }

    returnedClass.AnotherExampleMethod();
}

Tất cả những gợi ý trên thực sự chỉ là những nguyên tắc cơ bản của .NET Type, để biết thêm thông tin tôi khuyên bạn nên chọn CLR qua C # hoặc đọc bài viết MSDN này của cùng một tác giả – Jeffrey Richter. Ngoài ra, hãy kiểm tra, ví dụ phức tạp hơn nhiều khi bạn có thể gặp NullReferenceException.

Đọc Thêm:  Gọi Tên CPU Intel Core i9-10850K - Con Cưng Của Game Thủ Cao Cấp

Chúc các bạn thành công !

1 http://maps.google.com/url?q=https://10az.net/
2 http://www.google.de/url?q=https://10az.net/
3 https://www.google.co.jp/url?q=https://10az.net/
4 http://www.google.it/url?q=https://10az.net/
5 http://www.google.es/url?q=https://10az.net/
6 http://www.google.co.uk/url?q=https://10az.net/
7 http://images.google.es/url?q=https://10az.net/
8 http://images.google.co.uk/url?q=https://10az.net/
9 https://www.google.pl/url?q=https://10az.net/
10 https://www.google.com.br/url?q=https://10az.net/
11 http://www.google.pl/url?q=https://10az.net/
12 http://www.google.nl/url?q=https://10az.net/
13 http://www.google.com.br/url?q=https://10az.net/
14 http://www.google.com.au/url?q=https://10az.net/
15 http://www.google.co.in/url?q=https://10az.net/
16 http://www.google.ca/url?q=https://10az.net/
17 http://images.google.pl/url?q=https://10az.net/
18 http://images.google.nl/url?q=https://10az.net/
19 http://images.google.com.br/url?q=https://10az.net/
20 http://images.google.com.au/url?q=https://10az.net/
21 https://www.google.ru/url?q=https://10az.net/
22 https://www.google.be/url?q=https://10az.net/
23 http://www.google.se/url?q=https://10az.net/
24 http://www.google.ru/url?q=https://10az.net/
25 http://www.google.cz/url?q=https://10az.net/
26 http://www.google.com.tw/url?q=https://10az.net/
27 http://www.google.ch/url?q=https://10az.net/
28 http://www.google.at/url?q=https://10az.net/
29 http://images.google.se/url?q=https://10az.net/
30 http://images.google.ru/url?q=https://10az.net/
1 http://maps.google.com/url?q=https://daydore.com/
2 http://www.google.de/url?q=https://daydore.com/
3 https://www.google.co.jp/url?q=https://daydore.com/
4 http://www.google.it/url?q=https://daydore.com/
5 http://www.google.es/url?q=https://daydore.com/
6 http://www.google.co.uk/url?q=https://daydore.com/
7 http://images.google.es/url?q=https://daydore.com/
8 http://images.google.co.uk/url?q=https://daydore.com/
9 https://www.google.pl/url?q=https://daydore.com/
10 https://www.google.com.br/url?q=https://daydore.com/
11 http://www.google.pl/url?q=https://daydore.com/
12 http://www.google.nl/url?q=https://daydore.com/
13 http://www.google.com.br/url?q=https://daydore.com/
14 http://www.google.com.au/url?q=https://daydore.com/
15 http://www.google.co.in/url?q=https://daydore.com/
16 http://www.google.ca/url?q=https://daydore.com/
17 http://images.google.pl/url?q=https://daydore.com/
18 http://images.google.nl/url?q=https://daydore.com/
19 http://images.google.com.br/url?q=https://daydore.com/
20 http://images.google.com.au/url?q=https://daydore.com/
21 https://www.google.ru/url?q=https://daydore.com/
22 https://www.google.be/url?q=https://daydore.com/
23 http://www.google.se/url?q=https://daydore.com/
24 http://www.google.ru/url?q=https://daydore.com/
25 http://www.google.cz/url?q=https://daydore.com/
26 http://www.google.com.tw/url?q=https://daydore.com/
27 http://www.google.ch/url?q=https://daydore.com/
28 http://www.google.at/url?q=https://daydore.com/
29 http://images.google.se/url?q=https://daydore.com/
30 http://images.google.ru/url?q=https://daydore.com/

Trả lời