Search code examples
c#exceptionnulltextboxargumentnullexception

how to define null array in c#


What I want to do is to edit the database text file with the data in the text box, and if the text box is empty or whitespace data won't be changed. Reading and Writing methods in the database works properly I just need to pass the data in a string array to my method so it will be written as comma separated file. I also used IsNullOrWhitespace but still getting this exception error.

private void AdminEditsNewStudentButton_Click(object sender, EventArgs e)
    {
       
        string[] data= null;
       
        if (this.StudentIDTextBox.Text != null || Directory.Exists(@"database\\Students\\" + this.StudentIDTextBox.Text))
        {
            Admin obj = new Admin();
            data[0] = string.IsNullOrWhiteSpace(this.NewFirstNameTextBox.Text) ? " " : this.NewFirstNameTextBox.Text;
            data[1] = string.IsNullOrWhiteSpace(this.NewLastNameTextBox.Text) ? " " : this.NewLastNameTextBox.Text;
            data[2] = string.IsNullOrWhiteSpace(this.NewPhoneTextBox.Text) ? " " : this.NewPhoneTextBox.Text;
            data[3] = string.IsNullOrWhiteSpace(this.NewEmailTextBox.Text) ? " " : this.NewEmailTextBox.Text;
            data[4] = string.IsNullOrWhiteSpace(this.NewSuiteTextBox.Text) ? " " : this.NewSuiteTextBox.Text;
            data[5] = string.IsNullOrWhiteSpace(this.NewStreetNumTextBox.Text) ? " " : this.NewStreetNumTextBox.Text;
            data[6] = string.IsNullOrWhiteSpace(this.NewPostalCodeTextBox.Text) ? " " : this.NewPostalCodeTextBox.Text;
            obj.AdminEditsStudent(this.StudentIDTextBox.Text,data);
            MessageBox.Show("Student's info was modified", "Successful Write");
            this.Close();
        }
        else
        {
            MessageBox.Show("Student ID not found!");

        }
       
    }

enter image description here


Solution

  • Initialize your stringarray data.

    Replace:

    string[] data= null; 
    

    with

    string[] data = new string[7];