[英]Check if object is null before calling method. Object reference not set to an instance of an object
我不斷收到NullReferenceException。 如何初始化雙打以確定它們是否為空? 對不起,菜鳥問題:(
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);
要回答您的獎勵問題:
滑塊的上一個值由RoutedPropertyChangedEventArgs<T>
提供,並存儲在屬性OldValue
。
要回答您的問題:
您的代碼中唯一可引發NullReferenceException
是在未初始化slider1
且該slider1
為null
。 我假定UI調用了方法slider1_ValueChanged
,以便初始化參數sender
和e
。 因此,請嘗試附加調試器,以查看引發異常的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.