user1071461
user1071461

Reputation:

Confusing NullReferenceException

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

Upvotes: 0

Views: 232

Answers (3)

Lex Li
Lex Li

Reputation: 63143

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.

Upvotes: 1

Jan
Jan

Reputation: 2168

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.

Upvotes: 1

penguinvasion
penguinvasion

Reputation: 156

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.

Upvotes: -1

Related Questions