none
Object reference not set to an instance of an object. RRS feed

  • Question

  • Hi.

    I know there is an easy and logical explenation to my problem, but I cant find it.

    So: I have a form named Person, with comboBox1 for name, texBox1 for username, textBox2 for password and texBox3 for role. ComboBox1 has a datasource on table Oseba with the same coloumns as above plus IDP (primary key). DisplayMember on comboBox1 is name and ValueMember is IDP.
    I select the name from comboBox1 and using comboBox1_SelectedValueChanged it fills the rest of the textBoxes with their values.
    The form actually works good, the problem is, when I close the form. it throws me the "famous": Object reference not set to an instance of an object.

    Code:

    private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
            {
               
                ordinacijadbDataSet.PersonRow foundRow=
                ordinacijadbDataSet.Person.FindByIDP(Convert.ToInt32(comboBox1.SelectedValue));

                textBox1.Text = foundRow.Username.ToString();   <----- THE ERROR POINTS HERE
                textBox2.Text = foundRow.Password.ToString();
                textBox3.Text = foundRow.Role.ToString();
          
            }

    As I said, the form works like it should, until i try to close it. I cant figure out the problem.

    Plese help
    Thursday, September 6, 2007 11:16 AM

Answers

  • I would guess that at the point where there error occurs that there is no found row. You probably need to check whether this object is null before attempting to reference it.

     

    Thursday, September 6, 2007 1:42 PM
  • The foundRow object is probably being set to null during the Form unload process. You could probably set your code to break in debug when the foundRow object changes to null, which would explain why and how it's happening.

    Thursday, September 6, 2007 2:58 PM

All replies

  • What is the object foundRow?

     

    Thursday, September 6, 2007 12:30 PM
  • foundRow is a specific row in table Person. I find it with help of the primary key, which  I get from comboBox1.SelectedValue.
    From "foundRow" I then extract data and insert it into the rest of text.Boxes.

    I do this, if I want to change any of the values, like username.

    First I display all the info about specific person, change the value and then save it back in table using:

    private void Update_Click(object sender, EventArgs e) {

    ordinacijadbDataSet.PersonRow foundRow =
    ordinacijadbDataSet.Person.FindByIDP(Convert.ToInt32(comboBox1.SelectedValue));
                 
    foundRow.Username = textBox1.Text;
    foundRow.Password = textBox2.Text;
    foundRow.Role = textBox3.Text;

    this.personTableAdapter.Update(this.ordinacijadbDataSet.Person);

    MessageBox.Show("Person updated.");
    }


    Thursday, September 6, 2007 1:23 PM
  • I would guess that at the point where there error occurs that there is no found row. You probably need to check whether this object is null before attempting to reference it.

     

    Thursday, September 6, 2007 1:42 PM
  • Great. You were right. I just inserted a null check and it works.

    But why, how?

    If the form works and does what it has to do, it means that the foundRow should not be empty.

    Anyway, thx a lot.
    Thursday, September 6, 2007 2:42 PM
  • The foundRow object is probably being set to null during the Form unload process. You could probably set your code to break in debug when the foundRow object changes to null, which would explain why and how it's happening.

    Thursday, September 6, 2007 2:58 PM