Rajat
Rajat

Reputation: 323

Null reference error in slider element

I have a slider element in my windows phone app which has min value=1 and max value=10 and by default the value is 1 when page loads. I want the value of the slider to be shown in a text block. so 1st I am storing the value of the slider in an integer when the values of slider changes using ValueChanged event. I have following code in this event handler.

private void complexitySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
          int complexity;
          complexity =(int)complexitySlider.Value; //<--null reference
        }

but when I run my application, it shows the NullReference error. I have cross checked the name of the slider, its correct. How can I remove this bug? I don't want to directly bind the text block with slider value

Upvotes: 0

Views: 762

Answers (1)

Anton Sizikov
Anton Sizikov

Reputation: 9230

well, it might help you to avoid NRE.

private void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> args)
{
   int complexity;
   var slider = sender as Slider;
   if(slider != null) 
   {
     complexity =(int)slider.Value;
   }
}

Upvotes: 1

Related Questions