C# throw object reference not set to an instance of an object

Dung Do Tien Oct 29 2021 135

Hello, I have created a small project in the .Net 5 console application in VS Code.

I want to work with objects and a list of objects. I have created a class as below:

using System;
using System.Collections.Generic;
using System.Linq;

namespace StudentManager
{
    class Student
    {
       public int Id { get; set; }
       public string Fullname { get; set; }
       public string Email { get; set; }

       public Student Create(int id, string fullname, string email){
           return new Student(){Id = id, Fullname = fullname, Email = email};
       }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>();
            if(students != null){
                var student = students.FirstOrDefault();
                Console.WriteLine(student.Id);
                Console.WriteLine(student.Fullname);
                Console.WriteLine(student.Email);
            }
        }
    }
}

But run this code with command dotnet run I got an error: System.NullReferenceException: Object reference not set to an instance of an object.

PS C:\Project\LearnCShape\StudentManager> dotnet run
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at StudentManager.Program.Main(String[] args) in C:\Project\LearnCShape\StudentManager\Program.cs:line 25
PS C:\Project\LearnCShape\StudentManager> 

I checked null but the exception still throws when my list object is empty.

Thanks for any suggestions.

Have 2 answer(s) found.
  • B

    Bhavani Naidu Oct 29 2021

    You can see students variable not  NULL but It's only an empty array. So your condition check null only is not enough. 

    You can check more length of it by using Any() method as below:

    if(students != null && students.Any()){
       var student = students.FirstOrDefault();
       Console.WriteLine(student.Id);
       Console.WriteLine(student.Fullname);
       Console.WriteLine(student.Email);
    }

    And your exception will be resolved.

  • M

    Manish Kumar Oct 29 2021

    I think students variable is an empty list so when you get the top 1 object from that list by using FirstOrDefault() command will return NULL. And when an object is null you will not access the properties of that object.

    To fix this exception you can check the null object before using it:

    List<Student> students = new List<Student>();
    if(students != null){
       var student = students.FirstOrDefault();
       if(student != null){
           Console.WriteLine(student.Id);
           Console.WriteLine(student.Fullname);
           Console.WriteLine(student.Email);
       }
    }

    It's so simple, I hope it helpful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close