1

I keep getting a NullReferenceException. How do I initialize my doubles to determine if they are null? Sorry for the noob question :(

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{

    //gets new value of slider
    double newValue = (double)Math.Round(e.NewValue);

    //gets current/previous value of slider
    double  currentValue = slider1.Value;

    //gets sum of previous value - new value 
    double valueSum = (double)Math.Round(currentValue - newValue);
Mark
  • 8,015
  • 1
  • 11
  • 29
Special_K
  • 21
  • 2
  • Bonus Question: How do I save the previous value of the slider before it has been dragged to a new value? I have tried to use GetValue but it has not worked. – Special_K Jul 27 '15 at 23:15
  • 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) – Ben N Jul 28 '15 at 00:22

1 Answers1

0

To answer your Bonus question:

The previous value of the slider is provided by the RoutedPropertyChangedEventArgs<T> and it is stored in the property OldValue.

To answer your question:

The only place in your code that can throw a NullReferenceException is when slider1 is not initialized and is null. I assume that the method slider1_ValueChanged is called by the UI, so that parameter sender and e are initialized. So try to attach a debugger an see where the Exception is thrown.

Jehof
  • 32,386
  • 9
  • 115
  • 149