Reputation: 3775
My purpose is to add two buttons which allow the user to jump to the first of to the last item quickly. I'm using MVVM path and the code is very simple:
Sub ScrollDown()
If ResponseModel.Items.Count > 0 And ResponseModel.IsDataLoaded Then
If ResponseModel.Items.LastOrDefault IsNot Nothing Then ResponseList.ScrollTo(ResponseModel.Items.LastOrDefault())
End If
End Sub
Sometimes this code throws a NullReferenceException on the last line, yes, the one with End Sub
. None of these object are null, so I can't find out what the problem is.
System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Phone.Controls.LongListSelector.ScrollTo(Object item, Nullable`1 isGroup) at Microsoft.Phone.Controls.LongListSelector.ScrollTo(Object item) at WindowsPhoneAnswers.Thread.Lambda$_68() at WindowsPhoneAnswers.Thread.Lambda$_67(Object a0, EventArgs a1) at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
The only possible explanation is that the last item hasn't been realized yet, but how to check it?
Upvotes: 1
Views: 103
Reputation: 3775
The solution sounds weird as well. It seems that calling the method using the Dispatcher
solves the issue.
However this is odd since the exception was raised randomly and I'm not on a background thread.
Upvotes: 1