If the conditions do not close correctly

I have a condition ifthat evaluates an email address. I am testing 2 things

  • It has an @ sign
  • After the first @, it ends in "gmail.com".

My understanding of short circuiting is that if the first condition in the OR (||) operator is true, then the second will not be evaluated. And yet I get NullPointerExceptionit because the second statement gets the grade (I tested it).

What I do not understand about the short circuit here?

The corresponding code is given below:

FYI: emailtext is an EditText element, in which case it is "notavalidemail"

String[] split = emailtext.split("@", 2);
final AlertDialog.Builder notifier = new AlertDialog.Builder(this);
Log.i("GetToken","Split[0]=" + split[0] + "; length=" + split.length);

if (split.length == 1 || !split[1].equals("gmail.com")) {
    //Tell the user they didn't enter a correct gmail
}

Entering the code returns the following:

05-02 13:51:16.447  27553-27553/com.farmsoft.lunchguru.app I/GetToken﹕ Split[0]=notavalidemail; length=1
+4
source share
1

, :

    String[] split = emailtext.split("@", 2);

    final AlertDialog.Builder notifier = new AlertDialog.Builder(this);
    Log.i("GetToken","Split[0]=" + split[0] + "; length=" + split.length);

    boolean errorMail = true;
    if(split.length == 2)
    {
        if(split[1].equals("gmail.com"))
        {
            errorMail = false;
        }
    }
    if(errorMail)
    {
        // Mail error
    }
0

Source: https://habr.com/ru/post/1539040/


All Articles