Why setting ComboBox.SelectedValue to null causes an ArgumentNullException?

Why does setting a SelectedValue from ComboBox to null raise an ArgumentNullException ?

An exception occurs only if the ComboBox is actually part of the form. I can set SelectedValue for all value types or types that don't make sense, but I can't set it to null .

It is not that SelectedValue cannot be null . In fact, its value is null at the time I'm trying to set it to null .

In my real code, this does not happen in the constructor, and I do not explicitly null . The code uses a variable that is null . I can fix this by checking the non- null variable before trying to set SelectedValue . But I do not understand why I can not set the value to null .

Code Editing : DataSource now contains an element where ValueMembers is actually null

 using System.Collections.Generic; using System.Windows.Forms; public class Form1 : Form { public Form1() { var comboBox1 = new ComboBox(); Controls.Add(comboBox1); comboBox1.ValueMember = "Key"; comboBox1.DisplayMember = "Value"; comboBox1.DataSource = new List<Record> { new Record {Key = "1", Value = "One"}, new Record {Key = null, Value = "null"} }; comboBox1.SelectedItem = null; // no problem comboBox1.SelectedValue = ""; // no problem comboBox1.SelectedValue = new object(); // no problem comboBox1.SelectedValue = null; // ArgumentNullException!! } } public class Record { public string Key { get; set; } public string Value { get; set; } } 
+7
source share
5 answers

Checking the implementation of a property in Reflector as follows:

 public object SelectedValue { get { if ((this.SelectedIndex != -1) && (this.dataManager != null)) { object item = this.dataManager[this.SelectedIndex]; return this.FilterItemOnProperty(item, this.valueMember.BindingField); } return null; } set { if (this.dataManager != null) { string bindingField = this.valueMember.BindingField; if (string.IsNullOrEmpty(bindingField)) { throw new InvalidOperationException(SR.GetString("ListControlEmptyValueMemberInSettingSelectedValue")); } PropertyDescriptor property = this.dataManager.GetItemProperties().Find(bindingField, true); int num = this.dataManager.Find(property, value, true); this.SelectedIndex = num; } } } 

So this seems to depend on the fact that this.dataManager not null.

If this.dataManager not null, the setter will call Find() with key set to the value you set SelectedValue to:

 internal int Find(PropertyDescriptor property, object key, bool keepIndex) { if (key == null) { throw new ArgumentNullException("key"); } if (((property != null) && (this.list is IBindingList)) && ((IBindingList) this.list).SupportsSearching) { return ((IBindingList) this.list).Find(property, key); } if (property != null) { for (int i = 0; i < this.list.Count; i++) { object obj2 = property.GetValue(this.list[i]); if (key.Equals(obj2)) { return i; } } } return -1; } 

What will throw an exception if key is null.

I assume that the dataManager set only to a non-zero value when the ComboBox is inserted into the container (e.g. Form), so it will not explode when it is not in the container.

(Actually, dataManager will be non-null if you set the Control.DataSource property to non-zero.)

However, this does not seem to be correct because you reported a NullReferenceException , and this throws an ArgumentNullException explicitly.

[EDIT] It really was an ArgumentNullExeption ; OP is updated accordingly.

+5
source

My guess was that this might be skipped over what should be an ArgumentNullException , not a NullReferenceException (selected in the setterter implementation).

While quickly checking the code that you provided (plus the Main method with var form1 = new Form1() ), I found that I was not actually getting a NullReferenceException as you describe, but rather an ArgumentNullException as I expect. follows.

Are you sure you specified the type of exception correctly?

UPDATE:

As Matthew Watson describes, the parameter indicated by ArgumentNullException is actually key .

+2
source

Perhaps because if the ValueMember of SelectedValue property is Nothing, then SelectedValue will return .ToString () to SelectedItem.

0
source

In button_click, this code has been modified and removed by ArgumentNullException :

 if (comboBoxPays.SelectedValue == null) { id_pays = 0; } else id_pays = int.Parse(comboBoxPays.SelectedValue.ToString()); 
0
source

I know this is an old question, but now I have the same error and I found that I can not set SelectedValue = null, but I managed to set it to DBNull.Value

0
source

All Articles