Aishwarya Shiva
Aishwarya Shiva

Reputation: 3406

Unknown exception when validating password field in Xamarin Forms Cross Platform App

I am trying to validate a text and a password field for empty values using following code:

if (txtUsername.Text.Trim().Length > 0 && txtPassword.Text.Trim().Length > 0)
{
    //Some code for successful validation
}
else
    DisplayAlert("Required Fields Empty", "Please enter username and password.", "OK");

The code is executing nicely when I remove && txtPassword.Text.Trim().Length > 0 from it. But with this code included, it's showing following exception:

An unhandled exception occured.

And no other detail for this exception is mentioned. So, please if someone could tell me why this is happening and where can I find logs that may contain exception details?

UPDATE

After using try...catch, I found that the Text property of password field is NULL. What could be the reason?

Upvotes: 0

Views: 126

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 77012

txtPassword.Text is null. Change your code to:

if ((txtUsername.Text != null) && (txtPassword.Text != null) && (txtUsername.Text.Trim().Length > 0) && (txtPassword.Text.Trim().Length > 0))

if you want the else to be executed in the case one of them is null. Otherwise use try-catch. The reason of txtPassword.Text being null should be searched for either in the initialization or in your code, if somewhere it is being set to null.

Upvotes: 1

Related Questions