Peter Black
Peter Black

Reputation: 69

How to Object Array to List

(C#) I have 2 classes. 1 is called Employee. The other is my "main". I am trying to take a list and assign each value in list to an array of Employee object.

//Inside "Main" class
int counter = NameList.Count;

        Employee[] employee = new Employee[counter];

        for (int i = 0; i <= counter; i++)
        {
            employee[i].Name = NameList[i];
            employee[i].EmpNumber = EmpNumList[i];
            employee[i].DateOfHire = DOHList[i];
            employee[i].Salary = SalaryList[i];
            employee[i].JobDescription = JobDescList[i];
            employee[i].Department = DeptList[i];
        }

This returns the error:

An unhandled exception of type 'System.NullReferenceException' occurred in Pgm4.exe Additional information: Object reference not set to an instance of an object.

I think this means that I am not calling the list properly. Any help would be much appreciated. Thank you.

Upvotes: 1

Views: 227

Answers (4)

ChrisF
ChrisF

Reputation: 137138

As well as needing to create an instance of Employee for each element of the array, this line also has an error:

for (int i = 0; i <= counter; i++)

C# arrays are zero based so you need:

for (int i = 0; i < counter; i++)

otherwise you'll loop past the end of the array.

Your code should look like this:

for (int i = 0; i < counter; i++)
{
    employee[i] = new Employee();
    .... // your existing code here
}    

Upvotes: 0

Stephen Byrne
Stephen Byrne

Reputation: 7485

One thing that immediately stands out here. You are not declaring employee[i] to be new Employee at the start of each iteration therefore all other things aside, you will get a NullReferenceException

(Also as Marc Gravell pointed out, <= should be < to avoid an Index out of range exception...)

for (int i = 0; i < counter; i++)
        {
         //actually create an instance of Employee in employee[i]
            employee[i]  = new Employee();
            employee[i].Name = NameList[i];
            employee[i].EmpNumber = EmpNumList[i];
            employee[i].DateOfHire = DOHList[i];
            employee[i].Salary = SalaryList[i];
            employee[i].JobDescription = JobDescList[i];
            employee[i].Department = DeptList[i];
        }

Upvotes: 5

Yosi Dahari
Yosi Dahari

Reputation: 6999

Employee isn't initialized, add this to the beginning of each iteration:

employee[i]  = new Employee();

You are accessing an index that isn't allocated for you

replace this:

int i = 0; i <= counter; i++

with:

int i = 0; i < counter; i++

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062745

You have created an array with space for the references, but you have not created any Employee instances. All your Employee[i] are null. It should be:

for (int i = 0; i < counter; i++)
{
    var emp = new Employee();
    emp.Name = NameList[i];
    emp.EmpNumber = EmpNumList[i];
    emp.DateOfHire = DOHList[i];
    emp.Salary = SalaryList[i];
    emp.JobDescription = JobDescList[i];
    emp.Department = DeptList[i];
    employee[i] = emp;
}

or (tidier)

for (int i = 0; i < counter; i++)
{
    employee[i] = new Employee
    { 
        Name = NameList[i],
        EmpNumber = EmpNumList[i],
        DateOfHire = DOHList[i],
        Salary = SalaryList[i],
        JobDescription = JobDescList[i],
        Department = DeptList[i]
    };
}

Upvotes: 3

Related Questions