對於這樣一個簡單的問題,我表示歉意,但我一直在努力修復此代碼幾個小時,現在我無法取得任何進展。在另一個類中的類中填充列表
我有一個項目需要使用三個類:一個客戶,一個員工和一個客戶組。這些類從文件(.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