less than 1 minute read

The LongListSelector loads perfectly on first load; when navigated ahead and back, the application crashes with NullReferenceException

Error

NullReferenceException

StackTrace:

at Microsoft.Phone.Controls.LongListSelector.Balance() at Microsoft.Phone.Controls.LongListSelector.LongListSelector_LayoutUpdated(Object sender, EventArgs e) at System.Windows.FrameworkElement.OnLayoutUpdated(Object sender, EventArgs e) at MS.Internal.JoltHelper.RaiseEvent(IntPtr target, UInt32 eventId, IntPtr coreEventArgs, UInt32 eventArgsTypeIndex)

Now solution,

  • Open the toolkit code
  • Navigate to Balance() function
  • Add additional condition in the IF statement to make it appear like this

private void Balance()
{
if (!IsReady() || _flattenedItems == null || _flattenedItems.Count == 0)
....
....
}

Recompile and try the same scenario, it won't crash now!