SegmentedControl SelectedIndex issue - NullReferenceException

6 posts, 0 answers
  1. Martin
    Martin avatar
    18 posts
    Member since:
    Oct 2018

    Posted 26 Nov 2018 Link to this post

    Hello,

    by default SegmentedControls SelectedIndex is -1. In my code SegmentedControls ItemsSource is databound to the view model, because the buttons are generated dynamically. If you initialize SelectedIndex with 0 (or greater) before the ItemsSource databound property is allocated in the view model (= null), SegementedControl throws a NullReferenceException. On valid ItemsSource and if you set SelectedIndex to any number greater than the number of items in ItemsSource a NullReferenceException is thrown also.

    Xamarin.Forms.Picker uses SelectedIndex too. It ignores the SelectedIndex value if ItemsSource is null or in case SelectedIndex exceeds the number of items in ItemSource, the last item is selected.


    Best regards,

    Martin

  2. Lance | Principal TSE
    Admin
    Lance | Principal TSE avatar
    1043 posts

    Posted 26 Nov 2018 Link to this post

    Hello Martin,

    This is expected behavior as you cannot set an index that is out of the available range (the exception should be an OutOfRangeException instead).

    To clarify about the -1 value, is the typical default SelectedIndex when nothing is selected. Such is the case when there are no items yet. If you have a view model property two-way bound to the control, you should always initialize that property to -1 instead of 0. The SegementedControl will change it to 0 after the ItemsSource is set.

    private mySelectedIndex = -1;
     
    public MySelectedIndex
    {
        get => mySelectedIndex;
        set => SetProperty(ref mySelectedIndex, value);
    }


    On the other side of things, you should always check the item count before setting an index:

    var indexToSet = 5;
     
    if(indexToSet > myItems.Count - 1)
    {
        indexToSet = myItems.Count - 1;
    }
     
    SelectedIndex = indexToSet;

    Feature Request

    Although the Xamarin.Forms Picker lets you set an out of bound value, and it shifts to the next available value, this is not the typical behavior of items controls. If there isn't an index available, it should not select an invalid location instead (first or last item).

    In any case, I have submitted a feature request to development team to consider adding a Picker-like behavior: SegmentedControl SelectedIndex Bounds.  Please take a moment to up-vote/follow the item and leave a comment if you have any other info you'd like to add.

    I hope this info was helpful.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Martin
    Martin avatar
    18 posts
    Member since:
    Oct 2018

    Posted 27 Nov 2018 in reply to Lance | Principal TSE Link to this post

    Hello Lance,

    thank you for your message. You are right, SelectedIndex is databound to my view model too and I initialized it with 0, but the ItemsSource was loaded async via Task in my view model. That's why ItemsSource was still null when Segmented Control read the databound SelectedIndex. I fixed my view model immediately. I think the NullReferenceExceptions is thrown from the underlying framework, because Segemented Control accesses it's ItemsSource without null-check, that's why I was writing here.

    What I know from my experience with WPF, controls do not throw exceptions on problematic values coming from databinding. I think they see databinding more or less as a lightweight or loose connection, which does not throw hard exceptions, but ignores.

    The example with setting SelectedIndex to an out of bounds value was just a test I ran to see what Segmented Control does and what Framework controls do, it was not from my programming.

    That's all, thank you and have a nice day.

    Regards

    Martin
  4. Chuck Giddens
    Chuck Giddens avatar
    2 posts
    Member since:
    Nov 2006

    Posted 19 Sep Link to this post

     Great...a crash for now reason.  I battled this for a full day.  Why not just ignore it instead of this low level error.  This is a terrible design.
  5. Yana
    Admin
    Yana avatar
    4897 posts

    Posted 19 Sep Link to this post

    Hi Chuck,

    I am sorry for the negative experience you had with RadSegmentedControl.

    I completely agree the behavior could be improved and the exception should be handled internally.  We have this logged in the referenced feedback item and will definitely take care of it when working on the issue.

    Regards,
    Yana
    Progress Telerik

  6. Chuck Giddens
    Chuck Giddens avatar
    2 posts
    Member since:
    Nov 2006

    Posted 19 Sep Link to this post

    Yes it is just really hard to debug these types of exceptions in Xamarin XAML.  If it threw a better error message instead of just NULL OBJECT ERROR.  Maybe like "index references an object that does not exist in your data source".  Just something a little more descriptive

     

Back to Top