0
votes

I thought I had fixed this, but it doesn't seem to be working anymore. I have a picker that looks like this:

                        <StackLayout Padding="15">
                            <Label Text="Choose a Victory"
                                   FontSize="Subtitle"
                                   FontAttributes="Bold"
                                   TextColor="{StaticResource TitleFontColour}"
                                   FontFamily="{StaticResource MontSerrat}"/>
                            <Picker x:Name="quickPicker" 
                                    Title="- Select -"
                                    TitleColor="White"
                                    HorizontalOptions="FillAndExpand"
                                    TextColor="#8759D7"
                                    FontFamily="{StaticResource MontSerrat}"
                                    ItemDisplayBinding="{Binding Desc}"
                                    SelectedItem="{Binding SelectedDesc}">
                            </Picker>
                        </StackLayout>

The picker is based on:

        public Task<List<QuickVictories>> GetQuickVictoriesAsync()
        {
            return _database.Table<QuickVictories>().OrderByDescending(x => x.DisplaySeq).ToListAsync();
        }

My Model is:

MainModel.cs

    public class QuickVictories
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        [MaxLength (140)]
        public string Desc { get; set; }
        [AutoIncrement]
        public int DisplaySeq { get; set; }
    }

My look behind AddVictory.xaml.cs

protected override async void OnAppearing()
        {
            base.OnAppearing();

            quickPicker.ItemsSource = await App.Database.GetQuickVictoriesAsync();
        }

        async void OnSaveButtonClicked(object sender, EventArgs e)
        {

            var selectedQuickVictory = quickPicker.SelectedItem as QuickVictories;
            var quickVictory = selectedQuickVictory.Desc;

            var victory = new TheVictory()
            {
                Title = title.Text ?? quickVictory ?? "No title",
                Quick = quickVictory ?? "N/A",
                Details = details.Text ?? "No details were entered.",
                Date = DateTime.UtcNow
            };

            await App.Database.SaveVictoryAsync(victory);

            await DisplayAlert(
                "You have just celebrated a Victory!",
                "Your Victory has been saved for future celebrations.",
                "Woohoo!"
            );

            await Navigation.PopAsync();
        }

Whenever I try to save using OnSaveButtonClicked I get the error specifically on this line: var quickVictory = selectedQuickVictory.Desc;

This method is currently working on my production version of the app that is live in the Google Play store, so I am very confused about why this isn't working anymore.

Does anyone have any idea where I can look to try and fix this because I'm fresh out of ideas.

The goal, to be clear, is that I get the Desc from the QuickVictories model. I have tried casting the object to string, but it doesn't seem to be working since moving to the picker being the return of a sqlite query.

Edit:

Uploading a pic of the SelectedItem debug.

SelectedItem Debug Menu

1
that doesn't make sense because there is no cast on that line. If you look in the debugger what is the actual type of quickPicker.SelectedItem?Jason
@Jason It's an object. I know there's no Cast there, and I think I may have confused things: I have tried casting before (i.e. var selectedQuickVictory = (string)quickPicker.SelectedItem;) but I that stopped working as soon as the picker used the output of a SQLite query instead of using a static list I generated, I have no idea why.ScottishRoss91
So are you getting an exception/error, or is it just "not working"? The debugger should show you the actual type, not just object.Jason
The error is: System.NullReferenceException: 'Object reference not set to an instance of an object.' I've added an image of the message I get when hovering over SelectedItem. I believe it is an object, as most posts/tutorials I've looked at says I need to cast it to a string, as that's what I want to save it as. But it just... Isn't working. The method you see in the above post is currently working in a production version of the app that's available on the Play store, and it seems to work. I'm just so confused about why this isn't working anymore.ScottishRoss91
SelectedItem will be null if nothing has been selected. Are you sure that you have selected a picker item? You should probably also get rid of SelectedItem="{Binding SelectedDesc}"Jason

1 Answers

0
votes

you need to check if SelectedItem is null

if (picker.SelectedItem == null)

and display a message to the user, or whatever is appropriate for your use case