Array help Index out of range exeption was unhandled

Posted by Michael Quiles on Stack Overflow See other posts from Stack Overflow or by Michael Quiles
Published on 2010-06-07T16:16:54Z Indexed on 2010/06/07 16:22 UTC
Read the original article Hit count: 371

Filed under:

I am trying to populate combo boxes from a text file using comma as a delimiter everything was working fine, but now when I debug I get the "Index out of range exeption was unhandled" warning. I guess I need a fresh pair of eyes to see where I went wrong, I commented on the line that gets the error //Fname = fields[1];

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace Sullivan_Payroll
{
    public partial class xEmpForm : Form
    {
        bool complete = false;

        public xEmpForm()
        {
            InitializeComponent();
    }

    private void xEmpForm_Resize(object sender, EventArgs e)
    {
        this.xCenterPanel.Left = Convert.ToInt16((this.Width - this.xCenterPanel.Width) / 2);
        this.xCenterPanel.Top = Convert.ToInt16((this.Height - this.xCenterPanel.Height) / 2);
        Refresh();

    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //Exits the application
        this.Close();
    }


    private void xEmpForm_FormClosing(object sender, FormClosingEventArgs e) //use this on xtrip calculator
    {
        DialogResult Response;

        if (complete == true)
        {
            Application.Exit();

        }
        else
        {
            Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            if (Response == DialogResult.No)
            {

                complete = false;
                e.Cancel = true;


            }

            else
            {
                complete = true;
                Application.Exit();

            }

        }

    }

    private void xEmpForm_Load(object sender, EventArgs e)
    {
        //file sources
        string fileDept = "source\\Department.txt";
        string fileSex = "source\\Sex.txt";
        string fileStatus = "source\\Status.txt";


        if (File.Exists(fileDept))
        {

            using (System.IO.StreamReader sr = System.IO.File.OpenText(fileDept))
            {
                string dept = "";
                while
                    ((dept = sr.ReadLine()) != null)
                {
                    this.xDeptComboBox.Items.Add(dept);

                }


            }

        }
        else
        {
            MessageBox.Show("The Department file can not be found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        if (File.Exists(fileSex))
        {
            using (System.IO.StreamReader sr = System.IO.File.OpenText(fileSex))
            {
                string sex = "";
                while
                    ((sex = sr.ReadLine()) != null)
                {
                    this.xSexComboBox.Items.Add(sex);

                }


            }

        }
        else
        {
            MessageBox.Show("The Sex file can not be found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        if (File.Exists(fileStatus))
        {
            using (System.IO.StreamReader sr = System.IO.File.OpenText(fileStatus))
            {
                string status = "";
                while
                    ((status = sr.ReadLine()) != null)
                {
                    this.xStatusComboBox.Items.Add(status);

                }


            }

        }
        else
        {
            MessageBox.Show("The Status file can not be found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }



    private void xFileSaveMenuItem_Click(object sender, EventArgs e)
    {
        {
            const string fileNew = "source\\New Staff.txt";
            string recordIn;
            FileStream outFile = new FileStream(fileNew, FileMode.Create, FileAccess.Write);

            StreamWriter writer = new StreamWriter(outFile);

            for (int count = 0; count <= this.xEmployeeListBox.Items.Count - 1; count++)
            {

                this.xEmployeeListBox.SelectedIndex = count;
                recordIn = this.xEmployeeListBox.SelectedItem.ToString();
                writer.WriteLine(recordIn);

            }
            writer.Close();
            outFile.Close();

            this.xDeptComboBox.SelectedIndex = -1;
            this.xStatusComboBox.SelectedIndex = -1;
            this.xSexComboBox.SelectedIndex = -1;
            MessageBox.Show("your file is saved");

        }

    }

    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();

            }

            reader.Close();
            inFile.Close();

            if (this.xEmployeeListBox.Items.Count >= 1)
            {
                this.xFileSaveMenuItem.Enabled = true;
                this.xFilePrintMenuItem.Enabled = true;
                this.xEditClearMenuItem.Enabled = true;

            }

            else
            {
                this.xFileSaveMenuItem.Enabled = false;
                this.xFilePrintMenuItem.Enabled = false;
                this.xEditClearMenuItem.Enabled = false;
                MessageBox.Show("Records not found");
            }

        }




    private void xEditClearMenuItem_Click(object sender, EventArgs e)
    {
        this.xEmployeeListBox.Items.Clear();
        this.xDeptComboBox.SelectedIndex = -1;
        this.xStatusComboBox.SelectedIndex = -1;
        this.xSexComboBox.SelectedIndex = -1;
        this.xFileSaveMenuItem.Enabled = false;
        this.xFilePrintMenuItem.Enabled = false;
        this.xEditClearMenuItem.Enabled = false;
    }

}

}

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

© Stack Overflow or respective owner

Related posts about c#