0

I have created two comboboxes, one for the min value and one for the max value. My code should make sure the user doesn't select a min value greater than the max value, or a max value smaller than the min value using this code.

private void MaxRating_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (MaxRating.SelectedIndex)
            {
                case 0:
                    if (MinRating.SelectedIndex > 0)
                        MinRating.SelectedIndex = 0;
                    break;
                case 1:
                    if (MinRating.SelectedIndex > 1)
                        MinRating.SelectedIndex = 1;
                    break;
                case 2:
                    if (MinRating.SelectedIndex > 2)
                        MinRating.SelectedIndex = 2;
                    break;
                case 3:
                    if (MinRating.SelectedIndex > 3)
                        MinRating.SelectedIndex = 3;
                    break;
                case 4:
                    if (MinRating.SelectedIndex > 4)
                        MinRating.SelectedIndex = 4;
                    break;
            }
        }

However when debugging at the line where it says "if (MinRating.SelectedIndex > 0)" I get "NullReferenceException was unhandled by user code".

I'm not sure why, I also have a function for MinRating_SelectionChanged, and I don't seem to be getting anything like that there.

If I remove case:0 from this function, there seems to be no errors. Not from the other function either. I also tried replacing > with == but it just seems to do the same thing. Any help would be appereciated because I'm just confused.

EDIT: if I just place these two lines

int minrating = MinRating.SelectedIndex;
int maxrating = MaxRating.SelectedIndex;

it gives the error at the second line

  • 1
    Can you put the whole exception text in the question please – Preet Sangha Aug 08 '12 at 01:59
  • 1
    Once you solve that, you can also simplify your code to 1 line: `MinRating.SelectedIndex = Math.Min(MinRating.SelectedIndex, MaxRating.SelectedIndex);` – drch Aug 08 '12 at 01:59
  • Add a breakpoint to the line which throws the exception, and check each part of the expression for the null reference – Carson Myers Aug 08 '12 at 02:46
  • 1
    In regards to the edit: That means that there is no index selected for MaxRating. You are asking for the result of a selection that has not (yet) been made. – Jan Aug 08 '12 at 03:28
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nasreddine Oct 04 '15 at 09:07

3 Answers3

1

Can't you just say this?

if (MinRating.SelectedIndex > MaxRating.SelectedIndex)
{
    MinRating.SelectedIndex = MaxRating.SelectedIndex;
}

Then you don't try to read a value that has not been set yet. Also it is a lot shorter.

On a side node: You should not use the SelectedIndex property. Work with the values of your objects rather than with their positions in a collection.

Jan
  • 2,168
  • 2
  • 19
  • 28
  • That.. would be a lot easier yes.. and more clear.. and better in every way .. but I still have the error somehow –  Aug 08 '12 at 02:24
  • That only works if you actually have selected an item in MaxRating. – Jan Aug 08 '12 at 02:34
  • Well there are default items selected so that's not an issue –  Aug 08 '12 at 03:22
1

If carefully analyze, you will see this method may be called even during initialization of the controls, where MaxRating has just been constructed but MinRating is not even constructed. You cannot assume both of them are constructed when this method is called.

You might check against null for both of them at the beginning of this method as a workaround.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • 1
    Good point. Either you register the SelectionChanged event manually after both controls have been created or you make sure that you do not populate the controls until both controls have been created. In the latter case you also have to take into account that ALL elements have to be "in" the control, BEFORE you select one of them. – Jan Aug 08 '12 at 03:26
  • Yep that does seem to be the issue! Fixed it by putting a loaded event on MinRating and MaxRating and creating two bools MinRatingControlLoaded and MaxRatingControlLoaded and adding an if statement for these two bools to be true before trying to execute the code, thanks! –  Aug 08 '12 at 03:58
-1

I'm not that experienced with C#, but perhaps case 0 is executed when MaxRating.SelectedIndex is null, (0 being equivalent to null), and therefore it is entering the case with a null MaxRating.SelectedIndex.

penguinvasion
  • 156
  • 1
  • 8