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