Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未处理数组帮助索引超出范围异常_C# - Fatal编程技术网

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

C# 未处理数组帮助索引超出范围异常,c#,C#,我试图用逗号作为分隔符从文本文件填充组合框,一切正常,但现在当我调试时,出现“索引超出范围异常未处理”警告。我想我需要一双新的眼睛来看看哪里出了问题,我在获取错误的那一行上发表了评论//Fname=fields[1] private void xViewFacultyMenuItem_Click(object sender, EventArgs e) { const string fileStaff = "source\\Staff.txt"; 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

为了不想看到你发布的庞大代码的人,这里有相关的一点:

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);
            }
        }
    }
}
使用系统;
使用System.IO;
课堂测试
{
静态void Main()
{
string[]lines=File.ReadAllLines(“source\\Staff.txt”);
对于(int i=0;i
您很可能在输入文件的末尾有一个额外的空行,因此只有一个(空)字段,索引超出了索引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!
if(fields.length<6)
{
//当前记录就是问题所在!
}
其他的
{
Lname=字段[0];
//等等。
}
recordIn=reader.ReadLine();//一定要把这个放在else块后面!

在读取文件时,您应该经常这样做,因为通常会有前导或尾随的空行。

这么长的代码。至少给出一些callstack当您尝试访问“字段[1]”时,是否可以在调试器中运行它并查看“字段”是什么?通过在拆分后插入
Debug.Assert(fields.Length==6)
开始调试文件中的任何空行?为什么不使用调试器查看导致错误的行?@Michael:所有其他字段仍将在上一次迭代中填充-Lname为“”这一事实表明文件中有一个空行,正如其他地方所怀疑的那样。(请注意,在循环中声明局部变量会使这一点更清楚,因为您不会让上一次迭代中的变量混淆事情。)