I apologize for such a simple question, but I've been working on fixing this code for several hours now and I can't make any headway.
I have a project that calls for the usage of three classes: a customer, a employee, and a customer group. The classes are populated from file (.csv) and are then dumped back onto console displaying their contents. The customer group class which contains both a single instance of the employee class and a list of customer(s) as well as a couple of other lists that handle a couple of class unique variables. When dumping out my code I get some Unhandled Exception errors, and debugging has shown that the customer class within the customer group class is null. For all I know, there could be an entire range of other issues, but getting this list of a class populated is the problem.
The code itself is pretty long, so I'm going to try to trim it down for brevity's sake.
The Program bit in question:
while (dataArray[i] == "End")
{
int.TryParse(dataArray[i], out temp);
i++;
testGrp.Customers.Add(new Customer(temp, dataArray[i++], dataArray[i++], dataArray[i++], dataArray[i++]));
double.TryParse(dataArray[i], out doubleTemp);
testGrp.AmountSpent.Add(doubleTemp);
i++;
testGrp.SpendingLevel.Add(dataArray[i]);
i++;
}
The Customer Group Class:
public CustomerGrp(int groupId, Employee executive, List<Customer> customers, List<double> amountSpent, List<string> spendingLevel)
{
this.groupId = groupId;
this.executive = executive;
this.customers = customers;
this.amountSpent = amountSpent;
this.spendingLevel = spendingLevel;
}
And the customer class:
public Customer(int newId, string newName, string newPhoneNumber, string newAddress, string newMarried)
{
this.Id = newId;
this.Name = newName;
this.PhoneNumber = newPhoneNumber;
this.Address = newAddress;
this.Married = newMarried;
}
dataArray is an array generated from breaking the initial string from the csv file into smaller bits. It's not pretty, but it serves its purpose for now. Prior to this, the groupID and the executive bits have been handled, ending in a i++ to prep for the part shown.
I can get the Employee executive part populated without errors, but populating a list of classes within a class is something I can't quite get my head around. I think I'm doing it right, but most examples I can find don't quite fit this situation. I know my code is anything but pretty, but I'm just trying to establish basic functionality before I start cleaning up. Any help or suggestions would be appreciated.
EDIT
As asked, the message given in console is as follows:
System.NullReferenceException Object Reference Not Set To an instance of the object. at 'line' and 'line'.
The lines are:
DumpContents(testGrp);
and
static void DumpContents(CustomerGrp customerGrp)
{
Console.WriteLine("------- Customer Content -------");
Console.WriteLine(" Id: {0}", customerGrp.GroupId);
DumpContents(customerGrp.Executive);
foreach (Customer cust in customerGrp.Customers)
{
DumpContents(cust); // <- Exception Error line here
}
Console.WriteLine("--------------------------------");
}
EDIT
Included the overloaded DumpContents function:
static void DumpContents(Employee employee)
{
Console.WriteLine("------- Employee Content -------");
Console.WriteLine(" Id: {0}", employee.Id);
Console.WriteLine(" Name: {0}", employee.Name);
Console.WriteLine(" Start Date: {0}", employee.StartDate);
Console.WriteLine(" Rate: {0}", employee.GetRate());
Console.WriteLine(" Hours: {0}", employee.GetHours());
Console.WriteLine(" Pay: {0}", employee.CalcPay());
Console.WriteLine(" Tenure: {0} Years", employee.GetTenure());
Console.WriteLine("--------------------------------");
}
static void DumpContents(Customer customer)
{
Console.WriteLine("------- Customer Content -------");
Console.WriteLine(" Id: {0}", customer.Id);
Console.WriteLine(" Name: {0}", customer.Name);
Console.WriteLine(" Phone Number: {0}", customer.PhoneNumber);
Console.WriteLine(" Address: {0}", customer.Address);
Console.WriteLine("Marital Status: {0}", customer.Married);
Console.WriteLine("--------------------------------");
}