你调用的对象是空的。

时间:2014-03-15 21:38:18

标签: c#

获取“未将对象引用设置为对象实例”。错误。作为一个新手,我可能会忽视显而易见的......任何人都可以帮忙吗? (评论后面的错误行)感谢您对此事的任何进一步帮助。

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

}

2 个答案:

答案 0 :(得分:1)

这一行:

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

必须:

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

为了使用全球学生变量。

否则,您将创建一个局部变量,其范围仅限于方法btnAddName_Click()

答案 1 :(得分:0)

根据提供的信息,我猜测该学生为空。如果您访问空变量的成员,则会出现Object reference not set to an instance of an object错误。

由于