I need get value selected item, but write error in code line string imagePath = cbx_image.SelectedValue.ToString(); : object reference not set to an instance of an object
I add in my combobox:
cbx_image.Items.Add(new ComboBoxItem("Picture 1", imagePath));
cbx_image.Items.Add(new ComboBoxItem("Picture 2", imagePath));
and here i want get value:
private void btn_setImage_Click(object sender, EventArgs e)
{
string idButton = cbx_buttonID.Items[cbx_buttonID.SelectedIndex].ToString();
string imagePath = cbx_image.SelectedValue.ToString();
//change picture
toolBar1.SetButtonImage(idButton, imagePath);
}
And my class ComboBoxItem:
public class ComboBoxItem
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private object _value;
public object Value
{
get { return _value; }
set { _value = value; }
}
public ComboBoxItem(string name, string value)
{
_name = name;
_value = value;
}
public ComboBoxItem()
{ }
public override string ToString()
{
return Name;
}
}
Please help! How correct get value?
Loading

Dorababu MekaRank #457 · 5.5K reputationPosted Jul 25, 2011, 1:20 AM EDT
private void btn_setImage_Click(object sender, EventArgs e)
{
string idButton = cbx_buttonID.Items[cbx_buttonID.SelectedIndex].ToString();
ComboBoxItem cmb = (ComboBoxItem)cbx_image.SelectedItem;
string imagePath = cmb.Value.ToString();
toolBar1.SetButtonImage(idButton, imagePath);
}
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 25, 2011, 12:01 AM EDT
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 12:07 AM EDT
Please help
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 12:19 AM EDT
Looks like SelectedValue is null, i.e. nothing is selected in the combo box. Try accessing the value like this:
if (cbx_image.SelectedValue != null)
{
string imagePath = cbx_image.SelectedValue.ToString();
toolBar1.SetButtonImage(idButton, imagePath);
}
else
{
toolBar1.SetButtonImage(idButton, null); // supposing that SetButtonImage will not crash when receiving null for the image
}
Zoran
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 25, 2011, 12:24 AM EDT
string imagePath = cbx_image.SelectedItem.ToString();
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 12:28 AM EDT
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 12:35 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 25, 2011, 12:38 AM EDT
As per your code you are assigning a path but that did not get any file location as per you defined
myButton.BackgroundImage = System.Drawing.Image.FromFile(imagePath);
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 12:43 AM EDT
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 12:48 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 12:51 AM EDT
If you get object reference not set at that specific line, then either cbx_image is null or cbx_image.SelectedValue is null. Can you confirm this by debugging your application?
Zoran
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 12:56 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 1:00 AM EDT
cbx_image.Items.Add(new ComboBoxItem("Picture 1", imagePath));
cbx_image.Items.Add(new ComboBoxItem("Picture 2", imagePath));
cbx_image.SelectedIndex = 0;
This will select the first item in the list.
Zoran
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 1:04 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 1:06 AM EDT
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 1:07 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 1:12 AM EDT
Zoran
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 1:18 AM EDT
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 1:20 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 25, 2011, 1:22 AM EDT
Mike JonsonRank #7,584 · 238 reputationPosted Jul 25, 2011, 1:26 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 25, 2011, 1:30 AM EDT