0

I have an Android App in Xamarin Forms and in the app I have this buttons.
They all work perfectly, but the "cleanButtonPressed" method in the "btnClear" button have a little problem.

XAML:

<StackLayout VerticalOptions="EndAndExpand" HorizontalOptions="FillAndExpand" Orientation="Horizontal">
                <Button x:Name="btnSaveStock" Text="Salva" VerticalOptions="End" HorizontalOptions="FillAndExpand" Clicked="btnSaveStock_Clicked" IsEnabled="False"></Button>
                <Button x:Name="btnClear" Text="Pulisci" VerticalOptions="End" HorizontalOptions="FillAndExpand" Clicked="cleanButtonPressed" IsEnabled="False"></Button>
                <Button x:Name="btnCloseList" Text="Chiudi lista" VerticalOptions="End" HorizontalOptions="FillAndExpand" Clicked="btnCloseList_Clicked"></Button>
            </StackLayout>

When I press the button the method PulisciControlli() executes perfectly, and hits the breakpoint without any exception BreakpointSuccessful

But when I press F11 it generates immediately the exception.
NullReferenceException

PulisciControlli, if not called from this method, works perfectly. They are not present binding, resources dictionary... ecc in the XAML.

Thanks for the help!

EDIT:
PulisciControlli actually doing this:

private void PulisciControlli()
        {
            etyArticolo.Text = "";
            etyGiacenza.Text = "";
            etyRealGiacenza.Text = "";
            etyArticolo.Text = "";
            etyArticolo2.Text = "";
            etyFornitore.Text = "";

            matricolaSelezionata = null;
            ubicazioneSelezionata = null;

            pckUbicazioni.ItemsSource = null;
            pckUbicazioni.IsVisible = false;

            pckMatricole.ItemsSource = null;
            pckMatricole.IsVisible = false;

            etyArticolo2.IsEnabled = false;
            etyFornitore.IsEnabled = false;

            stcArticolo2.IsVisible = false;
            stcFornitore.IsVisible = false;
            stcOtherFields.IsVisible = false;

            etyArticolo.Focus();
        }
  • Could you share stack trace from the exception? – user2250152 Apr 06 '22 at 10:04
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – TheTanic Apr 06 '22 at 10:07
  • Stacktrace reports only "(External Code)" – Ascanio Donati Apr 06 '22 at 10:08
  • Nope, TheTanic I am not acting on null properties, and when the exception generates is after the closed bracket of the button clicked method – Ascanio Donati Apr 06 '22 at 10:10
  • please do not post code or errors as images – Jason Apr 06 '22 at 14:06
  • Yes, it was to make to understand that the cursor hits the close bracket successfully before crashing – Ascanio Donati Apr 06 '22 at 14:32
  • We need to figure out which line causes the error , did you try to add breakpoint inside the method `PulisciControlli` ? If problem persists , you can remove all the code from the method and see the result . – ColeX Apr 07 '22 at 02:04
  • Hi cole, I tried and the method PulisciControlli works well as you can see from the first screenshot. Trying to remove PulisciControlli from the button and leaving the method blank, the button has no problems. But PulisciControlli called from other method does not give problems, it had problems only called from the button – Ascanio Donati Apr 07 '22 at 07:25
  • This is weird , could you provide a basic , minimal ,reproducible project to use for test ? You can just upload to github and attach the link here . – ColeX Apr 08 '22 at 01:26

0 Answers0