2013-02-19 67 views
0

对于这样一个简单的问题,我表示歉意,但我一直在努力修复此代码几个小时,现在我无法取得任何进展。在另一个类中的类中填充列表

我有一个项目需要使用三个类:一个客户,一个员工和一个客户组。这些类从文件(.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("--------------------------------"); 
    } 
+0

欢迎来到[so],你可以发布'未处理的异常错误'吗?当你说*,但在课堂上填充一个班级列表是我无法理解的 - *你能否详细说明,因为你似乎正在这样做。我会把它作为答案 – 2013-02-19 23:54:43

+0

我已经添加了错误信息以及有问题的代码。至于人口稠密,这就是困扰我的东西,据我所知,它应该工作。这就是说,我确定要承担一些小小的不幸。 – user2088808 2013-02-20 00:05:14

+0

是的,看到这行'DumpContents(customerGrp.Executive)'它递归地调用DumpContents方法 - 但是与一个Employee然后再次在一个Customer类型的循环中。 DumpContents必须通过CustomerGrp。 – 2013-02-20 00:08:00

回答

0

线DumpContents(customerGrp.Executive )它递归地调用DumpContents方法 - 但是使用Employee类型,然后再次在Customer类型的循环中调用。 DumpContents必须通过CustomerGrp。

简单的解决将是超载DumpContents方法倾倒出来differrent类型的信息,如:

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("--------------------------------"); 
} 


static void DumpContents(Employee employee) 
{ 
    Console.WriteLine("------- Employee Content -------"); 
    Console.WriteLine("   Id: {0}", employee.Id); 
    ... 
} 


static void DumpContents(Customer customer) 
{ 
    Console.WriteLine("------- CustomerContent -------"); 
    Console.WriteLine("   Id: {0}", customer.Id); 
    ... 
} 
0

确保你的程序循环之前初始化一个新的即时 testGrp.Customers =新名单() ;

,似乎像你跳过我在环

编辑:哇,你是好的,你知道我是用我的手机来回答这个问题。

我相信他得到错误是因为他的客户对象从未实例化过。通常,您不会在DumpContents行上发生错误,因为它是一个foreach循环,除非该线程之间共享该对象。你可能会得到在该线上,而不是以前的行错误:的foreach(在customerGrp.Customers客户卡斯特)

这就是为什么我问他,以确保客户对象实例化

但后来这仅仅是我的猜测...

+1

我不会downvote,但这没有任何意义,如果'testGrp'为空,错误将在这一行'DumpContents(customerGrp.Executive);'也跳过一个'我''不管它的'foreach'不是一个for(int i = 0 ...)最后,SE网站的目标是在未来几年成为知识和答案的资源。当你回答问题时,比如发送文本消息,你不会得到提升。你的回答像Jon Skeet一样专业,你会得到upvotes ... – 2013-02-20 00:39:49

相关问题