数组帮助索引超出范围异常未处理

时间:2010-06-07 16:16:54

标签: c#

我正在尝试使用逗号作为分隔符从文本文件填充组合框一切正常,但是现在当我调试时,我得到“索引超出范围异常未处理”警告。我想我需要一双新眼睛看看我哪里出错了,我评论了得到错误的行// 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

5 个答案:

答案 0 :(得分: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);
            }
        }
    }
}

答案 1 :(得分:1)

这可能是因为文本文件顶部出现空行。

答案 2 :(得分:1)

您是否检查过文本文件末尾的空行?

答案 3 :(得分: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!

在阅读文件时,您应该例行执行此操作,因为通常会有前导或尾随空行。

答案 4 :(得分:0)

您很可能在输入文件的末尾有一个额外的空行,因此只有一个(空)字段,使您的索引超出索引1的范围。