-2

when the entry box is empty and I pressed the button. I get this error message System.ArgumentNullException: 'Value cannot be null. Parameter name: input' I tried many ways to figure out

        public MainPage()
    {
        InitializeComponent();
    }
    protected override async void OnAppearing()
    {
        base.OnAppearing();
        listmembers.ItemsSource = await App.Database.GetMemberAsync();
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {

        var email = loginEmail.Text;
        var emailpattern = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$";


        if (Regex.IsMatch(email, emailpattern))
        {
            await App.Database.SaveMemberAsync(new Member
            {
                Name = loginEmail.Text,
            });
            loginEmail.Text =  string.Empty;
            listmembers.ItemsSource = await 
                App.Database.GetMemberAsync();
        }

        else
        {
            await DisplayAlert("Error", "Invaild Email", "ok");
        }
    }
}
  • Welcome to Stack Overflow. Presumably one of your method calls is passing in a null value - but we don't have the full stack trace, so we don't know which method is failing. (It might not even be a method you're calling directly.) Please show the *complete* stack trace. – Jon Skeet Feb 22 '22 at 20:48
  • you need to check `string.IsNullOrEmpty(email)` before attempting to use it – Jason Feb 22 '22 at 20:54
  • This *isn't* a duplicate of the NullReferenceException question - ArgumentNullException is different. – Jon Skeet Feb 22 '22 at 20:57

1 Answers1

0

you need to test for null

if (!string.IsNullOrEmpty(email) && Regex.IsMatch(email, emailpattern))
Jason
  • 86,222
  • 15
  • 131
  • 146
  • Thank you it works. what if i did this way if ' else if (!Regex.IsMatch(email, emailpattern)) { await DisplayAlert("Error", "Invaild Email", "ok"); } ' and gives me System.ArgumentNullException – Niall Jackson Feb 22 '22 at 22:12
  • the exception occurs because you are passing a null`email` object to `Regex.IsMatch` . That's why you need to test for null **before** you call `Regex.IsMatch` – Jason Feb 22 '22 at 22:15
  • It makes sense now thank you very much – Niall Jackson Feb 22 '22 at 22:34