2013-02-19 69 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

相關問題