exception on eventhandler SelectedIndexChanged

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

// ComboBox take data from a BindingSource

this.TextBox1.Text = this.ComboBox1.SelectedValue.ToString();

}

Why this code generate an exception NullReferenceException when closing the MainForm

if I make this..

this.ComboBox1.SelectedIndexChanged -= new System.EventHandler(this.ComboBox1_SelectedIndexChanged);

the exception is not generated but I haven't the event that I need !!!

Can anyone help me Thank all.



Answer this question

exception on eventhandler SelectedIndexChanged

  • Harry_Leboeuf

    using this code I've this text in the TextBox:

    System.Data.DataRowView

    I've also changed the property SelectedItem = "ID"; but it's the same... ! why this

    thanks bye


  • Mike Haro

    IanG wrote:

    I would expect the reason is that there is no selected item when the form closes.

    SelectedValue does two things. First, it retrieves the currently selected item. And then it reads the property specified in ValueMember from the selected item.

    So if there is no selected item, SelectedValue essentially means "read a property from an item that isn't there". So a NullReferenceException actually makes perfect sense. There is no selected item, so any attempt to read a property from it has the same effect as you'd get any time you tried to use an object that isn't there.

    If you look at the documentation for ComboBox.SelectedValue, the code snippet they show checks the SelectedIndex property first to see if there's anything selected. So in general you should do this

    Yes I've see the documentation.

    IanG wrote:

    check to see SelectedIndex indicates that there really is a selected item before you use SelectedValue.

    how I can Can you post a little code example

    Thanks a lot.


  • Bit Bucket

    OK, so the next thing to do is check what thing is actually causing the exception. From the code snippet you've shown, I can see 4 places that might be causing it, although only 3 of those are possible in practice.

    The first is that comboBox1 might be null, in which case your 'if' statement test will throw the exception.

    The second is that this.TextBox1 might be null, in which case trying to set its Text property will throw the exception.

    The third is that if comboBox1 were null, accessing its SelectedValue property would throw the exception. (It can't be this one though, because you'd already have hit the first case I describe here if comboBox1 is null.)

    The fourth is the SelectedValue is null, in which case the call to ToString would throw the exception.

    The first thing to do is find out which of these is causing the exception. Use the debugger to inspect these variables and find out which is null.

    Once you've done that, we can go about finding out why it is null.


  • MMDG

    Soe Moe wrote:

    Hi..

    Normally, the ComboBox1_SelectedIndexChanged event won't occur after Form closing event.

    Did you do anything in the Form closing event or closed event. (e.g. change DataSource property .) If you do this, SelectedIndexChanged event will occur. And If your data source is null or there is no value, ComboBox1.SelectedValue will be null. So the exception is thrown.

    Anyway., you can prevent like this.

    if (comboBox1.SelectedIndex != -1)

    this.textBox1.Text = comboBox1.SelectedValue.ToString();

    ..also apply that code the exception is generated


  • milicica

    This works

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

    {

    TextBox text = new TextBox();

    text.Text = comboBox1.SelectedItem.ToString();

    }


  • Kinlan

    Hi..

    Normally, the ComboBox1_SelectedIndexChanged event won't occur after Form closing event.

    Did you do anything in the Form closing event or closed event. (e.g. change DataSource property .) If you do this, SelectedIndexChanged event will occur. And If your data source is null or there is no value, ComboBox1.SelectedValue will be null. So the exception is thrown.

    Anyway., you can prevent like this.

    if (comboBox1.SelectedIndex != -1)

    this.textBox1.Text = comboBox1.SelectedValue.ToString();



  • Carver42

    The documentation you say you've read contains an example.

    It checks the selected index first. If it's -1 that means nothing is selected.


  • akin_l

    I would expect the reason is that there is no selected item when the form closes.

    SelectedValue does two things. First, it retrieves the currently selected item. And then it reads the property specified in ValueMember from the selected item.

    So if there is no selected item, SelectedValue essentially means "read a property from an item that isn't there". So a NullReferenceException actually makes perfect sense. There is no selected item, so any attempt to read a property from it has the same effect as you'd get any time you tried to use an object that isn't there.

    If you look at the documentation for ComboBox.SelectedValue, the code snippet they show checks the SelectedIndex property first to see if there's anything selected. So in general you should do this - check to see SelectedIndex indicates that there really is a selected item before you use SelectedValue.


  • exception on eventhandler SelectedIndexChanged