This is a migrated thread and some comments may be shown as answers.

SegmentedControl SelectedIndex issue - NullReferenceException

5 Answers 176 Views
SegmentedControl
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 26 Nov 2018, 03:57 PM

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

5 Answers, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 26 Nov 2018, 08:16 PM
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
0
Martin
Top achievements
Rank 1
answered on 27 Nov 2018, 09:37 AM
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
0
Chuck Giddens
Top achievements
Rank 1
answered on 19 Sep 2019, 11:22 AM
 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.
0
Yana
Telerik team
answered on 19 Sep 2019, 01:10 PM

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

0
Chuck Giddens
Top achievements
Rank 1
answered on 19 Sep 2019, 01:15 PM

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

 

Tags
SegmentedControl
Asked by
Martin
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Martin
Top achievements
Rank 1
Chuck Giddens
Top achievements
Rank 1
Yana
Telerik team
Share this question
or