[Windows Mobile Software Development] wesch00:
NullReferenceException

I have a real issue I cannot solve and need some advice. Since this happens after upgradingt to 1.35 (may be also 1.3, didn't recognize that), it may have something to do with changes according to other errors posted here.

In 1.2 there was an error on removing items from the list control. Thats's why I implemented it that way, that I clear the list control totally and rebuild it.

senseListCtrl.TopIndex = 0; // from a hint here in this forum

senseListCtrl.Clear();

// and then add all items

senseListCtrl.AddItem(myComboBoxItem);

that worked fine until 1.2.

Since I have to combo box item and the content of the second is depending on the selected value of the first I did something like:

comboBoxItem.Items.Clear();

comboBoxItem.Items.AddRange(newEntries);

comboBoxItem.SelectedValue = valueFromTheSelectedValueOfTheOtherComboBox;

if (comboBoxItem.SelectedIndex == -1)

{

comboBoxItem.SelectedIndex = 0;

}

That also worked fine. Since AddRange is no more supported in 1.3, I rewrote this and I'm adding each item in a loop.

The issue now is, when I'm trying to drop down that second combo, I get a NullReferenceException. This NullReferenceException I get also everywhere I do a senseListControl.Clear() and rebuild the items. That is in allmost all of my forms.

Then I tried not to clear the listControl, but only clear the items of the combo. This does not crash with the NullReferenceException. But the text of the previous assigned items is shown instead of the newly added one. The displayed text seems to be not updated. On dropping down, I have only the newly added items in the list, that is ok.

I provided here a small demo form, which shows the issue. After selecting a different item in the first combo, the app crashes with NullReferenceException. I hope this will help fixing the problem:

public partial class Form1 : Form
{
private SenseListControl listControl;
private SensePanelComboItem firstComboItem;
private SensePanelComboItem secondComboItem;

private int firstValue = 0;
private int secondValue = 0;

public Form1()
{
InitializeComponent();

listControl = new SenseListControl();
listControl.Dock = DockStyle.Fill;

Controls.Add(listControl);

firstComboItem = new SensePanelComboItem("firstCombo");
firstComboItem.ShowSeparator = true;
firstComboItem.OnSelectedValueChanged += firstComboItem_OnSelectedValueChanged;

secondComboItem = new SensePanelComboItem("secondCombo");
secondComboItem.ShowSeparator = true;
}

protected override void OnLoad(EventArgs e)
{
Initiate();
}

private void Initiate()
{
listControl.Clear();

firstComboItem.Items.Clear();
firstComboItem.Items.Add(new SensePanelComboItem.Item("item 1", 1));
firstComboItem.Items.Add(new SensePanelComboItem.Item("item 2", 2));
firstComboItem.Items.Add(new SensePanelComboItem.Item("item 3", 3));
firstComboItem.SelectedValue = firstValue;
if (firstComboItem.SelectedIndex == -1)
{
firstComboItem.SelectedIndex = 0;
firstValue = (int)firstComboItem.SelectedValue;
}

secondComboItem.Items.Clear();
secondComboItem.Items.Add(new SensePanelComboItem.Item("item " + firstValue.ToString() + "-1", 10 * firstValue + 1));
secondComboItem.Items.Add(new SensePanelComboItem.Item("item " + firstValue.ToString() + "-2", 10 * firstValue + 2));
secondComboItem.Items.Add(new SensePanelComboItem.Item("item " + firstValue.ToString() + "-3", 10 * firstValue + 3));
secondComboItem.SelectedValue = secondValue;
if (secondComboItem.SelectedIndex == -1)
{
secondComboItem.SelectedIndex = 0;
secondValue = (int)secondComboItem.SelectedValue;
}

listControl.AddItem(firstComboItem);
listControl.AddItem(secondComboItem);
}

void firstComboItem_OnSelectedValueChanged(object Sender, object Value)
{
firstValue = (int)Value;
Initiate();
}
}