1

i am getting the null reference exception when running the following code.i have created a class where return a collection of objects.Printing the values in the main class.

//main class where i am calling employee class.
class Program
    {
            static void Main(string[] args)
            {
                //Creating an object for employee class.
                Employees obje = new Employees();
                var obj2 = obje.EmployeeDetails();

                //looping through each object and printing the values.
                foreach (var emp in obj2)
                {
                    Console.WriteLine(emp.Employeeid);
                    Console.WriteLine(emp.Employeename);
                    Console.WriteLine(emp.Employeeage);
                }
                Console.ReadKey();
            }
        }


    //Employee class with some properties and function. 
     class Employees
        {
            public int Employeeid { get; set; }
            public string Employeename { get; set; }
            public string Employeeage { get; set; }


            public Employees[] EmployeeDetails()
            {
                Employees[] objEmployees = new Employees[2];

                objEmployees[0].Employeeage = "20";
                objEmployees[0].Employeeid = 101;
                objEmployees[0].Employeename = "Arjunan";

                objEmployees[1].Employeeage = "24";
                objEmployees[1].Employeeid = 102;
                objEmployees[1].Employeename = "Shiva";

                return objEmployees;
            }
        }

Please tell me where i am doing mistake.am newbie to the dotnet world.

John Saunders
  • 160,644
  • 26
  • 247
  • 397

8 Answers8

2

First you need to create element of an array with new keyword.

 objEmployees[0] = new Employees();
 objEmployees[0].Employeeage = "20";
Lev
  • 3,719
  • 6
  • 41
  • 56
1

Your Employees is a class. That's why it is a reference type. And if you create an array with a reference type, all array items initialized to null as a default.

That means your objEmployees[0] is null as a default and you try to set null's property. That's why you get NullReferanceException.

From Arrays (C# Programming Guide)

The default values of numeric array elements are set to zero, and reference elements are set to null.

As a solution, you might need to create a new Employees object (with new keyword) for your array items or you can use an array initializer like tvanfosson's answer.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Employees[] objEmployees = new Employees[2]; // This creates the array not the items in it.

Chriseyre2000
  • 2,053
  • 1
  • 15
  • 28
0

Do it like this:

    public Employees[] EmployeeDetails()
    {
        Employees[] objEmployees = new Employees[2];
        objEmployees[0] = new Employees();//Note to the new instance
        objEmployees[0].Employeeage = "20";
        objEmployees[0].Employeeid = 101;
        objEmployees[0].Employeename = "Arjunan";

        objEmployees[1] = new Employees();//Note to the new instance
        objEmployees[1].Employeeage = "24";
        objEmployees[1].Employeeid = 102;
        objEmployees[1].Employeename = "Shiva";

        return objEmployees;
    }

You have to instantiate your array items with new object (Employees) before accessing them, otherwise, it would be equals to null reference.

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
0

You need to instantiate the Employees class in the array before setting the values:

        public Employees[] EmployeeDetails()
    {
        Employees[] objEmployees = new Employees[2];

        objEmployees[0] = new Employees();
        objEmployees[0].Employeeage = "20";
        objEmployees[0].Employeeid = 101;
        objEmployees[0].Employeename = "Arjunan";

        objEmployees[1] = new Employees();
        objEmployees[1].Employeeage = "24";
        objEmployees[1].Employeeid = 102;
        objEmployees[1].Employeename = "Shiva";

        return objEmployees;
    }
André Bires
  • 251
  • 3
  • 7
0
 public Employees[] EmployeeDetails()
            {
                Employees[] objEmployees = new Employees[2];

                objEmployees[0].Employeeage = "20";
                objEmployees[0].Employeeid = 101;
                objEmployees[0].Employeename = "Arjunan";

                objEmployees[1].Employeeage = "24";
                objEmployees[1].Employeeid = 102;
                objEmployees[1].Employeename = "Shiva";

                return objEmployees;
            }

This method is causing the problem. When you create the array of Employees, all you are doing is allocating memory. You haven't actually created any Employees objects and placed them in the array.

So when you try to do objEmployees[0].Employeeage = "20", you are getting a NullReference exception because no Employees object exists at objEmployees[0].

dursk
  • 4,435
  • 2
  • 19
  • 30
0

The problem is that you are creating the array, but you're not creating the objects in the array before you attempt to use them. In this case a better way to handle it would be to use an array initializer.

 return new []
 {
     new Employee
     {
         EmployeeAge = 20,
         EmployeeId = 101,
         EmployeeName = "Arjunan",
     },
     new Employee
     {
         EmployeeAge = 24,
         EmployeeId = 102,
         EmployeeName = "Shiva",
     },
 };

Note I've made the names of the properties follow PascalCase and assumed that the age property really should be numeric, not string.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
0

Try this one

 public Employees[] EmployeeDetails()
    {
        var objEmployees = new Employees[2];
        objEmployees[0] = new Employees();
        objEmployees[0].Employeeage = "20";
        objEmployees[0].Employeeid = 101;
        objEmployees[0].Employeename = "Arjunan";

        objEmployees[1] = new Employees();
        objEmployees[1].Employeeage = "24";
        objEmployees[1].Employeeid = 102;
        objEmployees[1].Employeename = "Shiva";



        return objEmployees;

    }

You need to create an instance for each objEmployees

user3342876
  • 107
  • 2
  • 2
  • 6