xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.64k stars 1.88k forks source link

Cannot reload Listview datasource more than once, app close after error, Object reference not set to an instance of an object #15852

Open rhunoja opened 6 months ago

rhunoja commented 6 months ago

I had created a Listview of radiobutton, labels and a button binded using MVVM, at first time listview load well, but after execute CheckedChanged on group radiobutton, it make change on list datasource i call method that reload datasource but process is truncated and close app, on debugging it make error "Object reference not set to an instance of an object"

Steps to Reproduce

// MV class namespace InspeccionesHS_App.ViewModels.CheckINAMViewModel.SetCheckINAMViewModel { public class RequestList1ViewModel: ViewModelBase {

region Commands

    public INavigation Navigation { get; set; }
    public ICommand NextCommand => new AsyncCommand(NextFormAsync);
    public Command LoadRequestsCommand => new AsyncCommand(LoadListRequests);
    #endregion
    private List<INAMRequestFormModel> _requestList = new List<INAMRequestFormModel>();
    public List<INAMRequestFormModel> RequestList
    {
        get { return _requestList; }
        set { SetProperty(ref _requestList, value); }
    }
    public RequestList1ViewModel(INAMModel ins)
    {
        LoadListRequests();
    }
    public async Task LoadListRequests()
    {
        if (IsBusy)
            return;
        IsBusy = true;
        try
        {    // method tha load a list datasource 
            var ListItemRequestForm = new List<INAMRequestFormModel>();
            RequestList = getDataList(ListItemRequestForm);
        }
        catch (Exception ex)
        {
            await _dialogService.ShowError(ex.Message, "Error", "Aceptar", null);
            IsBusy = false;
        }
        finally
        {
            IsBusy = false;
        }
      }
}

}

// On view page i built

 <Grid HorizontalOptions="FillAndExpand">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <!--<StackLayout Grid.Row="0" Style="{StaticResource boxSubtitleLabel}" Orientation="Horizontal" BackgroundColor="{StaticResource NewmontBlue282}">
            <Label Text="Preparar" Style="{StaticResource subtitleLabel}"/>

        </StackLayout>-->
        <StackLayout Grid.Row="0" HorizontalOptions="FillAndExpand" Orientation="Vertical">
            <StackLayout Style="{StaticResource boxSubtitleLabel}" Orientation="Horizontal" BackgroundColor="{StaticResource NewmontBlue282}">
                <Label Text="General" Style="{StaticResource subtitleLabel}"/>
                <StackLayout Orientation="Horizontal" VerticalOptions="Center">
                    <Label Text="N/A" Style="{StaticResource subtitleLabel}" WidthRequest="30"/>
                    <CheckBox   CheckedChanged="OnCategoryCheck"></CheckBox>
                    <!--<CheckBox  IsChecked="{Binding Checked}" ></CheckBox>-->
                </StackLayout>
            </StackLayout>
            <ListView x:Name="listRes"
                    ItemsSource="{Binding RequestList}"
                    IsRefreshing="{Binding IsBusy, Mode=OneWay}"
                    SelectionMode="None"
                    BackgroundColor="Transparent"
                    HasUnevenRows="True"
                    HorizontalOptions="Center"
                    VerticalOptions="FillAndExpand">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                                    <StackLayout Padding="10,5,10,0" >
                                        <Label Text="{Binding Name}" Style="{StaticResource labelQuestionTitle}"/>
                                    </StackLayout>
                                    <StackLayout Padding="10,5,10,0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                                        <Grid RowSpacing="0" ColumnSpacing="0">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="24"/>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="15*"></ColumnDefinition>
                                                <ColumnDefinition Width="13*"></ColumnDefinition>
                                                <ColumnDefinition Width="13*"></ColumnDefinition>
                                                <ColumnDefinition Width="17*"></ColumnDefinition>
                                                <ColumnDefinition Width="16*"></ColumnDefinition>
                                                <ColumnDefinition Width="16*"></ColumnDefinition>
                                                <ColumnDefinition Width="10*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <StackLayout Grid.Row="0" Grid.Column="0" Orientation="Horizontal" Style="{StaticResource BoxRequestGrid}">
                                                <Label Text="#" VerticalOptions="Start" Padding="0" Margin="0" VerticalTextAlignment="Start" FontAttributes="Bold" FontSize="{StaticResource FontSize18}"/>
                                                <Label Text="{Binding ItemOrder}" Style="{StaticResource labelQuestionResult}"/>
                                            </StackLayout>
                                            <StackLayout Grid.Row="0" Grid.Column="1" Orientation="Horizontal" Style="{StaticResource BoxRequestGrid}">
                                                <Label Text="&#xf02B;" Style="{StaticResource labelQuestionTitle}">
                                                    <Label.FontFamily>
                                                        <OnPlatform x:TypeArguments="x:String" Android="fa-solid-900.ttf#Font Awesome 6 Free Solid" iOS="Font Awesome 6 Free Solid"/>
                                                    </Label.FontFamily>
                                                </Label>
                                                <Label Text="{Binding Discoveries}" Style="{StaticResource labelQuestionResult}"/>
                                            </StackLayout>
                                            <StackLayout Grid.Row="0" Grid.Column="2"  Orientation="Horizontal" Style="{StaticResource BoxRequestGrid}">
                                                <Label Text="&#xf03E;" Style="{StaticResource labelQuestionTitle}" VerticalTextAlignment="Center">
                                                    <Label.FontFamily>
                                                        <OnPlatform x:TypeArguments="x:String" Android="fa-solid-900.ttf#Font Awesome 6 Free Solid" iOS="Font Awesome 6 Free Solid"/>
                                                    </Label.FontFamily>
                                                </Label>
                                                <Label Text="{Binding Photos}" Style="{StaticResource labelQuestionResult}"/>
                                            </StackLayout>
                                           <StackLayout Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="3" RadioButtonGroup.GroupName="{Binding ItemOrder}" 
                                                         Orientation="Horizontal" Style="{StaticResource BoxRequestGrid}">
                                                <RadioButton Content="N/A" Value="1" IsChecked="{Binding NA}" Style="{StaticResource RadioButtonReply}" CheckedChanged="OnReplyRadioButtonCheckedChanged"/>
                                                <RadioButton Content="Si" Value="2" IsChecked="{Binding SI}" Style="{StaticResource RadioButtonReply}" CheckedChanged="OnReplyRadioButtonCheckedChanged"/>
                                                <RadioButton Content="No" Value="3" IsChecked="{Binding NO}" Style="{StaticResource RadioButtonReply}" CheckedChanged="OnReplyRadioButtonCheckedChanged"/>
                                            </StackLayout>
                                            <StackLayout Grid.Column="6" Grid.Row="0"  Orientation="Horizontal" VerticalOptions="CenterAndExpand" HorizontalOptions="Center">
                                                <Button Text="&#xf105;" TextColor="{StaticResource IconColor}" Clicked="OnDiscovery_clicked" VerticalOptions="CenterAndExpand"
                                                       BackgroundColor="Transparent" FontAttributes="Bold" HorizontalOptions="Start"
                                                       Padding="2,0,52,0" Margin="0" FontSize="{StaticResource FontSizeMedium}" >
                                                    <Button.FontFamily>
                                                        <OnPlatform x:TypeArguments="x:String" Android="fa-solid-900.ttf#Font Awesome 6 Free Solid" iOS="Font Awesome 6 Free Solid"/>
                                                    </Button.FontFamily>
                                                </Button>
                                            </StackLayout>

                                        </Grid>

                                    </StackLayout>
                                    <StackLayout HorizontalOptions="FillAndExpand" HeightRequest="3" BackgroundColor="LightGray"></StackLayout>
                                </StackLayout>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

            <StackLayout HorizontalOptions="FillAndExpand" Style="{StaticResource PaddingSection}" >
                <Button Text="Siguiente"  Style="{StaticResource buttonNav}" Command="{Binding NextCommand}"/>
            </StackLayout>
        </StackLayout>
    </Grid>

// I guess i make all implementation well, this is cs class from view page

public partial class RequestList1View : ContentPage { RequestList1ViewModel viewModel; public RequestList1View(INAMModel _item) { InitializeComponent(); BindingContext = viewModel = new RequestList1ViewModel(_item); viewModel.Navigation = Navigation; NavigationPage.SetBackButtonTitle(this, string.Empty); } protected override async void OnAppearing() { base.OnAppearing();
}
async void OnCategoryCheck(object sender, CheckedChangedEventArgs e) { var b = (CheckBox)sender;

        var reply = (b.IsChecked) ? "1" : "0";
        await viewModel.UpdateRequestSelected(reply);
        viewModel.LoadRequestsCommand.Execute(null);
    }

    void OnReplyRadioButtonCheckedChanged(object sender, CheckedChangedEventArgs e)
    {
        RadioButton button = sender as RadioButton;
        var _request = button.BindingContext as INAMRequestFormModel;
        _request.Reply = button.Value.ToString();        
    }

}

Expected Behavior

Reload List item and show changes on runtine after make checkedchanges

Actual Behavior

Basic Information

Environment

Show/Hide Visual Studio info ``` ```

Build Logs

System.NullReferenceException Mensaje = Object reference not set to an instance of an object.

0xC in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:157,13  C#
0x6 in System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__7_0 at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021,49  C#
0xC in Android.App.SyncContext. at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:36,19 C#
0xE in Java.Lang.Thread.RunnableImplementor.Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36,6 C#
0x8 in Java.Lang.IRunnableInvoker.n_Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-33/mcw/Java.Lang.IRunnable.cs:84,4 C#
0x8 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:22,5  C#

Screenshots

screenshot1

Reproduction Link

Workaround