我有一個ObservableCollection,我甚至首先檢查它以確保它具有元素。然而,我仍然得到一個nullReferenceException(有時候,這從來沒有遇到與winrt 8.1版本的問題,我正在改變它爲UWP。) 代碼如下,它給了我錯誤,其中a.Url是:System.NullReferenceException在linq排序OrderBy(a => a.Url),UWP
if (sTumblrblog_gv_list.Count != 0)
{
if (tumblogconfig.ShowNSFWBlogs)
sTumblrGridView.ItemsSource = sTumblrblog_gv_list.OrderBy(a => a.Url);
else
sTumblrGridView.ItemsSource = sTumblrblog_gv_list.OrderBy(a => a.Url).Where(a => a.IsNsfw == false);
}
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Tumblr-FIA
StackTrace:
at tumblr_fia.MainPage.<>c.<updatestats>b__47_1(sTumblrblog_gv a)
at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
at System.Runtime.InteropServices.WindowsRuntime.EnumeratorToIteratorAdapter`1.MoveN
ext()
at System.Runtime.InteropServices.WindowsRuntime.EnumeratorToIteratorAdapter`1.get_HasCurrent()
InnerException:
我明白這個錯誤我相信,但我檢查sTumblrblog_gv_list不是空值。我現在試試&捕獲。有時我仍然收到錯誤信息。
如果項目之一'sTumblrGridView'是'那麼'sTumblrblog_gv_list.OrderBy(a => a.Url)'仍然會拋出異常。可能需要'.Where(a => a!= null)'。 – juharr
我這麼認爲。它應該放置在排序之前 – Andection