对于这样一个简单的问题,我表示歉意,但我一直在努力修复此代码几个小时,现在我无法取得任何进展。在另一个类中的类中填充列表
我有一个项目需要使用三个类:一个客户,一个员工和一个客户组。这些类从文件(.csv)填充,然后转储回控制台显示其内容。包含员工类的单个实例和客户列表的客户组类,以及处理几个类别唯一变量的其他列表。当抛出我的代码时,我得到一些未处理的异常错误,并且调试显示客户组类中的客户类为空。据我所知,可能存在一系列其他问题,但是获得这个类的列表是问题。
代码本身很长,所以为了简洁起见,我会尽量修剪它。
所讨论的程序位:
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++;
}
客户组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;
}
而客户类:
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中是从断裂初始产生的阵列将csv文件中的字符串转换为较小的位。这并不漂亮,但现在它已经达到了目的。在此之前,groupID和执行位已经处理完毕,以i ++结束,以便为显示的部分做准备。
我可以让员工执行部分填充没有错误,但在类中填充类的列表是我无法理解的。我认为我做对了,但我能找到的大多数例子都不太适合这种情况。我知道我的代码非常漂亮,但我只是在开始清理之前尝试建立基本功能。任何帮助或建议,将不胜感激。
EDIT
作为问,在控制台给出的消息如下所示:
System.NullReferenceException对象参考未被设置为对象的实例。在'线路'和'线路'。
的线是:
DumpContents(testGrp);
和
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
包括重载DumpContents功能:
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("--------------------------------");
}
欢迎来到[so],你可以发布'未处理的异常错误'吗?当你说*,但在课堂上填充一个班级列表是我无法理解的 - *你能否详细说明,因为你似乎正在这样做。我会把它作为答案 – 2013-02-19 23:54:43
我已经添加了错误信息以及有问题的代码。至于人口稠密,这就是困扰我的东西,据我所知,它应该工作。这就是说,我确定要承担一些小小的不幸。 – user2088808 2013-02-20 00:05:14
是的,看到这行'DumpContents(customerGrp.Executive)'它递归地调用DumpContents方法 - 但是与一个Employee然后再次在一个Customer类型的循环中。 DumpContents必须通过CustomerGrp。 – 2013-02-20 00:08:00