Reputation: 23
I have a list of messages that is sorted by the selected option (time, name or group ID) from a ComboBox. I added a ComboBox with Items to my xaml:
<ComboBox x:Name="sort_dropdown" HorizontalAlignment="Left" Margin="828,219,0,0" VerticalAlignment="Top" Width="120" SelectedIndex="0">
<ComboBoxItem x:Name="time_sort" Content="Time" Selected="time_sort_Selected"/>
<ComboBoxItem x:Name="nickname_sort" Content="Nickname" Selected="nickname_sort_Selected"/>
<ComboBoxItem x:Name="groupId_sort" Content="Group ID" Selected="groupId_sort_Selected"/>
</ComboBox>
to have the list reordered, I wrote a function in the cs file:
private void reorder()
{
if (time_sort.IsSelected)
fill_chat(MessageSorting.DateSort(Globals.MSG_LOCAL_LIST, ascendingOrder));
else if (nickname_sort.IsSelected)
fill_chat(MessageSorting.UserNameSort(Globals.MSG_LOCAL_LIST, ascendingOrder));
else fill_chat(MessageSorting.GroupIdSort(Globals.MSG_LOCAL_LIST, ascendingOrder));
}
When Run, I get a NullReferenceException. And upon debugging I found that the objects name reference to null.
Examples: time_sort = null. sort_dropdown = null. and etc.
Changing attributes on the xaml doesn't help because there's no reference to read those attributes. Thanks.
Update: This isn't the main window, and I noticed when I run the code that this window class runs before I call the .show function on it. For some reason the code in it is executed without being called.
Update 2: I have the following code:
public MainLoggedWindow()
{
MessageBox.Show("Before initialization");
InitializeComponent();
MessageBox.Show("after initialization");
}
when running, the first MessageBox shows up but the second doesn't. the reorder() function is called from the following function:
private void order_dropdown_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ascendingOrder = !ascendingOrder;
reorder();
}
ascendingOrder is a bool that saves the current sorting order. There's another comboBox with 2 items (Ascending or Descending), Every time the user changes it's choice this function runs.
Update 3: Following the Call Stack while debugging shows that initializeComponent();
calls order_dropdown_SelectionChanged(...)
which calls reorder()
.
Solved.
Narrowing down the problem: initializeComponent();
Caused order_dropdown_SelectionChanged(...)
to run.
Explanation: On the ComboBox declaration in the xaml, selectedIndex
attribute is defined like this: SelectedIndex="0"
. So part of the initialization is creating the ComboBox and setting selectedindex=0
which instantly triggers the order_dropdown_SelectionChanged(...)
function. Now since the ComboBox items weren't created yet the name reference was null and the program crashed.
Solution: Don't define both selectedIndex
attribute and _SelectionChanged(...)
in the xaml. Personally I removed the selectedIndex
attribute and defined it programmatically after initializeComponent();
is called.
Upvotes: 0
Views: 79
Reputation: 5773
As part of the InitializeComponent
it is setting the selection of your order_dropdown
; but this is occurring before the named controls have been set up. So your reorder
method needs to defend against that by wrapping its current contents within a test for if(time_sort!=null)
as a way of determining whether the initialize has completed.
Upvotes: -1