0

Getting an "Object reference not set to an instance of an object." error. Being a newbie, I am probably overlooking the obvious...can anyone help? (the error line following the remark) Thanks for any further help on this matter.

public partial class frmAddNewStudent : Form
{
    public frmAddNewStudent()
    {
        InitializeComponent();
    }

    private Student student = null;

    public Student GetNewStudent()
    {
        this.ShowDialog();
        return student;
    }

    private void frmAddNewStudent_Load(object sender, EventArgs e)
    {
    }

    private void btnAddName_Click(object sender, EventArgs e)
    {
        if (StudentDB.Duplicate(txtName.Text))
        {
            txtName.Focus();
        }
        else
        {
            Student student = new Student(txtName.Text, "");
        }
    }

    private void txtName_TextChanged(object sender, EventArgs e)
    {
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        if (!HasName())
        {
            NoName();
            txtName.Focus();
        }
        else if (StudentDB.Duplicate(txtName.Text))
        {
            txtName.Focus();
        }
        else if (!HasScore())
        {
            NoScore();
            txtScore.Focus();
        }
        else
        {
            student = new Student(txtName.Text, txtScores.Text);
            this.Close();
        }

    }

    private void btnAddScore_Click(object sender, EventArgs e)
    {
        if (!HasName())
        {
            NoName();
            txtName.Focus();
        }
        else if (!HasScore())
        {
            NoScore();
            txtScore.Focus();
        }
        else
        {
            if (IsValidScore())
            {
                if (txtScores.Text == "")
                {
 // the following line is where I get the error
                    student.Scores += txtScore.Text;
                    txtScores.Text = student.Scores;
                    btnOk.Focus();
                }
                else
                {
                    student.Scores += (" " + txtScore.Text);
                    //student.Scores = string.Join(" ", txtScore.Text);
                    txtScores.Clear();
                    txtScores.Text = student.Scores;
                    btnOk.Focus();
                }
            }
            else
            {
                BadScore();
            }
        }
    }

    private void txtScores_TextChanged(object sender, EventArgs e)
    {
        Student student = new Student(txtName.Text, txtScores.Text);
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnClearScores_Click(object sender, EventArgs e)
    {
        if (!HasName())
        {
            NoName();
            txtName.Focus();
        }
        else if (!HasScore())
        {
            NoScore();
            txtScore.Focus();
        }
        else
        {
            foreach (int grade in student.Scores)
            {
                student.Scores.Remove(grade);
            }
        }
    }

    private bool HasScore()
    {
        return Validation.ScoreIsPresent(txtScore);
    }

    private bool HasName()
    {
        return Validation.NameIsPresent(txtName);
    }

    private bool IsValidScore()
    {
        return Validation.IsValid(txtScore);
    }

    public void NoName()
    {
        MessageBox.Show("Must have Student Name.");
        txtName.Focus();
    }

    public void BadScore()
    {
        MessageBox.Show("Score must be between 0 - 100.");
        txtScore.Focus();
    }

    public void NoScore()
    {
        MessageBox.Show("You must input as least one score.");
        txtScore.Focus();
    }

}
Jerry G
  • 39
  • 6
  • 1
    We can't see bold in code markdown... – Szymon Mar 15 '14 at 21:40
  • I don't see any initialization for the student object or the txtScores object. Could that be it? It's a little hard to tell from this snip. – richb01 Mar 15 '14 at 21:42
  • Either `student` is null or `txtScore` is null when it gets to that line, therefore you cannot access the `.Text` or `.Scores` methods. – crthompson Mar 15 '14 at 21:44

2 Answers2

1

This line:

Student student = new Student(txtName.Text, "");

Has to be:

student = new Student(txtName.Text, "");

In order to use the global student variable.

Otherwise, you will be creating a local variable whose scope is limited to the method btnAddName_Click()

thepirat000
  • 12,362
  • 4
  • 46
  • 72
0

Given the information provided I would guess that the student is null. If you access to a member of null variables you get Object reference not set to an instance of an object error.

Thanks