Answered NullRefrence Exception

  • 29 aprilie 2012 18:10
     
      Are cod

    Hi,I want to change the dataGridView.DataSource based on the selection of the combo box which has loaded from data base.but here i got null refrence exception.actually it happens when I close the program using the X button.but when I stop it from debug menu i don't get the exception.

    can you tell me what is the problem with this code.thanks.

    private void classComboBox_SelectedIndexChanged(object sender, EventArgs e) { StudentsOfClassTableAdapter studentsOfClassTableAdapter = newStudentsOfClassTableAdapter(); studentDataGridView.DataSource = studentsOfClassTableAdapter.GetData(classComboBox.SelectedValue.ToString());

    }

Toate mesajele

  • 30 aprilie 2012 03:16
    Moderator
     
     

    Where the Null reference exception come form, does the Visual Studio help you point it out?

    Why you post those codes out, can you ensure that the problem is caused by these codes' insertion? If so, then try to ensure this, try to comment out line by line and then test if the exception thrown.

    Without the project and the detailed exception information, it is hard to "guess" out the root cause.

    If there's any concern, please feel free to let me know.

    Best wishes,


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

  • 30 aprilie 2012 05:25
     
     
    studentsOfClass is a stored procedure in a sqlExpress DB that I have mapped it to my dataSet.when I Execute it in the Data base it works correctly.
    GetData(string) is a method that accepts a string parameter for the stored procedure.
    here, I get the exception at the end of last line.
    studentDataGridView.DataSource = studentsOfClassTableAdapter.GetData(classComboBox.SelectedValue.ToString()); //exception thrown here
  • 30 aprilie 2012 05:34
     
     

    Why do you use SelectedValue property of comboBox? Have you specify the ValueMember for comboBox If so, than is OK. If not, please use SelectedItem instead.

    What can really be your problem is, in GetData method. This must be null (not yet instantiated) if its not static.

    But as Mike said, we need a code to say more.


    Mitja

  • 1 mai 2012 19:06
     
      Are cod

    I changed a littel my code but the problem is still there:
    this where exception thrown :

    private void classComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
       StudentsOfClassTableAdapter studentsOfClassTableAdapter = new StudentsOfClassTableAdapter();
       studentDataGridView.DataSource = studentsOfClassTableAdapter.GetData((short)classComboBox.SelectedValue);
    }

    and this is combo box configuration code :

    // 
    // classComboBox
    // 
                this.classComboBox.DataSource = this.classBindingSource1;
                this.classComboBox.DisplayMember = "ClassName";
                this.classComboBox.FormattingEnabled = true;
                this.classComboBox.Location = new System.Drawing.Point(13, 10);
                this.classComboBox.Name = "classComboBox";
                this.classComboBox.Size = new System.Drawing.Size(343, 21);
                this.classComboBox.TabIndex = 8;
                this.classComboBox.ValueMember = "ClassID";
                this.classComboBox.SelectedIndexChanged += new System.EventHandler(this.classComboBox_SelectedIndexChanged);
    // 

    and this is the GetData(short) method which is created by VS.

    public virtual DaftarMoeinSQLDBDataSet.StudentsOfClassDataTable GetData(global::System.Nullable<short> ClassID) {
                this.Adapter.SelectCommand = this.CommandCollection[0];
                if ((ClassID.HasValue == true)) {
                    this.Adapter.SelectCommand.Parameters[1].Value = ((short)(ClassID.Value));
                }
                else {
                    this.Adapter.SelectCommand.Parameters[1].Value = global::System.DBNull.Value;
                }
                DaftarMoeinSQLDBDataSet.StudentsOfClassDataTable dataTable = new DaftarMoeinSQLDBDataSet.StudentsOfClassDataTable();
                this.Adapter.Fill(dataTable);
                return dataTable;
            }

    by the way , using SelectedItem instead of SelectedValue causes same exception and it makes application stop immediately.where SelectedValue just throws the exception only when I close the application using the red X button.funny?

    thank you again for your helps.



  • 2 mai 2012 10:15
    Moderator
     
     

    Before you force to close the application through the close button in the ControlBox on your Window, what operation you did, what method it would be running? I think this would be the key point.

    And from your code and the exception you got, it only can makes us think that you select nothing for your ComboBox, I don't know why the SelectedIndexChanged event handler method would be executed when you click on the close button, this would also can show us the clues.

    And if also do not know why this method would be called at that moment, then you can use the try-catch surround the problem codes, and then print out the Exception.StackTrace and show them to us, it would can be real helpful clues.


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

  • 2 mai 2012 12:19
     
     Răspuns

    Hi

    If you put a break point in your code above and close the form, you will notice that the selected index changed event is fired. But because the form is being closed, the selected index of the combobox is cleared (contents are being disposed and the selectedValue is being set to null, which is the reason why the event is being fired).  So before you get the value of the combobox you have to check the selected value for null first, so that you dont try to use the SelectedValue property when it is null (which is what's resulting in the exception). The following should work:

    private void classComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {

    if(classComboBox.SelectedValue != null)

    {

    StudentsOfClassTableAdapter studentsOfClassTableAdapter = newStudentsOfClassTableAdapter();
    studentDataGridView.DataSource = studentsOfClassTableAdapter.GetData(classComboBox.SelectedValue.ToString());

    }

    }


    "'Impossible' doesn't mean something cannot be done. It simply means no solution is known to the individual(s) in question." - Mbongo M. Mpongwana

    • Marcat ca răspuns de mortaza.mkr 3 mai 2012 06:47
    •