在这种情况下,如何初始化数组以避免NullReferenceException错误

时间:2014-08-21 22:42:30

标签: c# arrays nullreferenceexception

我收到此错误,我完全不知道如何摆脱它。我分配了数组,但似乎无法正确初始化它。我在13行NullReference错误上分配了数组。我读到了它,但在这种情况下,我不知道我做错了什么。

我有以下代码。

namespace A4
{
    public partial class frmAddStudent : Form
    {
        public frmAddStudent()
        {
            InitializeComponent();
        }

        // Declare variables to be used by both event handlers
        int CountStudents = 0;
        double Average = 0;
        Student[] ClassList = new Student[50];

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // Create new student and assign name etc provided by user
            Student _Student = new Student();
            _Student.Name = txtName.Text;
            _Student.Surname = txtSurname.Text;
            _Student.Age = Convert.ToInt32(txtAge.Text);
            _Student.ITMark = Convert.ToInt32(txtIT.Text);
            _Student.MathMark = Convert.ToInt32(txtEng.Text);
            _Student.EngMark = Convert.ToInt32(txtMath.Text);

            // Use the hasPassed method to display an appropriate message after the 
            // student has been added
            MessageBox.Show("Student added.");

            // Increase counter and display how many students added so far
            CountStudents++;

            Average = Convert.ToInt32(txtIT.Text) + Convert.ToInt32(txtEng.Text) + Convert.ToInt32(txtMath.Text);
            _Student.AverageMark = Average / 3;



            //Display Student's properties
            richTextBox1.Text += ("Student: " + Convert.ToString(CountStudents) +
                                 "\nName: " + _Student.Name +
                                 "\nSurname: " + _Student.Surname +
                                 "\nAge: " + _Student.Age +
                                 "\nStudent Average: " + Convert.ToString(_Student.AverageMark) + "\n" + "\n");


            //Add the newly added student to the ClassList array
            ClassList[CountStudents - 1] = _Student;

            //Clear the list
            txtSurname.Clear();
            txtName.Clear();
            txtAge.Clear();
            txtIT.Value = 0;
            txtEng.Value = 0;
            txtMath.Value = 0;
            txtName.Focus();
        }




        private void displayAll_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            for (int j = 0; j < ClassList.Count(); j++)
            {
                richTextBox1.Text += ("Student: " + Convert.ToString(j) +
                                     "\nName: " + ClassList[j].Name +
                                     "\nSurname: " + ClassList[j].Surname +
                                     "\nAge: " + ClassList[j].Age +
                                     "\nStudent Average: " + Convert.ToString(ClassList[j].AverageMark) +
                                     "\n" + "\n");
            }

            //MessageBox.Show("Display all students.");
        }
    }
}

2 个答案:

答案 0 :(得分:4)

我怀疑你的空引用是在displayAll_Click方法中抛出的。在该方法中,您迭代数组中的所有元素(即所有50个元素),无论他们是否有学生。这是因为Count()返回数组的长度,而不是其中包含Student的数组元素的数量。

您应该在CountStudents循环中使用学生计数变量for,或者更好,使用List<Student>,这对您的使用非常理想,并且不太容易出现这些问题比阵列。

答案 1 :(得分:1)

什么是&#34; Count()&#34;在循环中:

ClassList.Count();

您的意思是ClassList.Length;吗?

如果您动态填充数组,最好使用List<Student> ClassList,这样可以减少问题。