2010-06-07 78 views
2

我想從文本文件中使用逗號作爲分隔符組合框填充一切工作正常,但現在當我調試我得到「索引超出範圍異常未處理」警告。我想我需要一雙新鮮的眼睛來看看我出錯的地方,我評論的那一行得到錯誤// Fname = fields [1];陣列幫助索引超出範圍異常未處理

private void xViewFacultyMenuItem_Click(object sender, EventArgs e) 
    { 
     const string fileStaff = "source\\Staff.txt"; 
     const char DELIM = ','; 
     string Lname, Fname, Depart, Stat, Sex, Salary, cDept, cStat, cSex; 
     double Gtotal; 
     string recordIn; 
     string[] fields; 

      cDept = this.xDeptComboBox.SelectedItem.ToString(); 
      cStat = this.xStatusComboBox.SelectedItem.ToString(); 
      cSex = this.xSexComboBox.SelectedItem.ToString(); 
      FileStream inFile = new FileStream(fileStaff, FileMode.Open, FileAccess.Read); 
      StreamReader reader = new StreamReader(inFile); 

      recordIn = reader.ReadLine(); 

      while (recordIn != null) 
      { 

       fields = recordIn.Split(DELIM); 
       Lname = fields[0]; 
       Fname = fields[1]; // this is where the error appears 
       Depart = fields[2]; 
       Stat = fields[3]; 
       Sex = fields[4]; 
       Salary = fields[5]; 

       Fname = fields[1].TrimStart(null); 
       Depart = fields[2].TrimStart(null); 
       Stat = fields[3].TrimStart(null); 
       Sex = fields[4].TrimStart(null); 
       Salary = fields[5].TrimStart(null); 

       Gtotal = double.Parse(Salary); 

       if (Depart == cDept && cStat == Stat && cSex == Sex) 
       { 

        this.xEmployeeListBox.Items.Add(recordIn); 

       } 
       recordIn = reader.ReadLine(); 

      } 




Source file -- 


Anderson, Kristen, Accounting, Assistant, Female, 43155 
Ball, Robin, Accounting, Instructor, Female, 42723 
Chin, Roger, Accounting, Full, Male,59281 
Coats, William, Accounting, Assistant, Male, 45371 
Doepke, Cheryl, Accounting, Full, Female, 52105 
Downs, Clifton, Accounting, Associate, Male, 46887 
Garafano, Karen, Finance, Associate, Female, 49000 
Hill, Trevor, Management, Instructor, Male, 38590 
Jackson, Carole, Accounting, Instructor, Female, 38781 
Jacobson, Andrew, Management, Full, Male, 56281 
Lewis, Karl, Management, Associate, Male, 48387 
Mack, Kevin, Management, Assistant, Male, 45000 
McKaye, Susan, Management, Instructor, Female, 43979 
Nelsen, Beth, Finance, Full, Female, 52339 
Nelson, Dale, Accounting, Full, Male, 54578 
Palermo, Sheryl, Accounting, Associate, Female, 45617 
Rais, Mary, Finance, Instructor, Female, 27000 
Scheib, Earl, Management, Instructor, Male, 37389 
Smith, Tom, Finance, Full, Male, 57167 
Smythe, Janice, Management, Associate, Female, 46887 
True, David, Accounting, Full, Male, 53181 
Young, Jeff, Management, Assistant, Male, 43513 
+0

這麼長的代碼。至少給一些callstack – brickner 2010-06-07 16:19:22

+0

你可以在調試器中運行它,看看當你試圖訪問「fields [1]」時有什麼「字段」? – bentsai 2010-06-07 16:19:57

+0

在分割後插入'Debug.Assert(fields.Length == 6)'開始調試 – tanascius 2010-06-07 16:21:03

回答

5

對於任何人誰不想看你已經發布了聲勢浩大的代碼的緣故,這裏的相關位:

while (recordIn != null) 
{ 

    fields = recordIn.Split(DELIM); 
    Lname = fields[0]; 
    Fname = fields[1]; // this is where the error appears 

鑑於你已經看到了例外,那基本上表示recordIn不包含分隔符DELIM(逗號)。我建議你明確檢查預期的大小,並拋出一個異常,給出更多的細節,如果你得到一個不適當的行。或者,如果它是一條空白線路,正如其他人所建議的那樣(並且確實有可能),那麼您可能需要跳過它。

另外,這裏有一個簡短而完整的控制檯應用程序,它可以幫助你找到問題:

using System; 
using System.IO; 

class Test 
{ 
    static void Main() 
    { 
     string[] lines = File.ReadAllLines("source\\Staff.txt"); 
     for (int i = 0; i < lines.Length; i++) 
     { 
      string line = lines[i]; 
      string[] fields = line.Split(','); 
      if (fields.Length != 6) 
      { 
       Console.WriteLine("Invalid line ({0}): '{1}'", 
            i + 1, line); 
      } 
     } 
    } 
} 
+3

+1「爲了任何人的緣故」:) – bottlenecked 2010-06-07 18:25:55

0

你最有可能得到一個額外的空白行中輸入文件的末尾,因此這隻有一個(空的)字段,給您的索引超出範圍索引1.

1

這可能是因爲空行出現在文本文件的頂部。

1

您是否在文本文件末尾檢查了空行?

1

在此之後:

fields = recordIn.Split(DELIM); 

你需要這個:

if (fields.length < 6) 
{ 
    // the current recordIn is the problem! 
} 
else 
{ 
    Lname = fields[0]; 
    // etc. 
} 
recordIn = reader.ReadLine(); // make sure to put this after the else block! 

讀取文件時,你應該這樣做定期,因爲經常有開頭或結尾的空行。