In my trying AsyncTask
I get email address from my server. In onPostExecute()
I have to check is email address empty
or null
String myString = null;
if (TextUtils.isEmpty(myString)) {
return; // or break, continue, throw
}
// myString is neither null nor empty if this point is reached
Log.i("TAG", myString);
Notes
From @Jon Skeet comment, really the String
value is "null"
. Following code solved it
if (userEmail != null && !userEmail.isEmpty() && !userEmail.equals("null"))
You can check it with utility method "isEmpty" from TextUtils,
isEmpty(CharSequence str) method check both condition, for null and length.
public static boolean isEmpty(CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
Yo can check it with this:
if(userEmail != null && !userEmail .isEmpty())
And remember you must use from exact above code with that order. Because that ensuring you will not get a null pointer exception from userEmail.isEmpty()
if userEmail
is null.
Above description, it's only available since Java SE 1.6. Check userEmail.length() == 0
on previous versions.
UPDATE:
Use from isEmpty(stringVal)
method from TextUtils
class:
if (TextUtils.isEmpty(userEmail))
Kotlin:
Use from isNullOrEmpty
for null or empty values OR isNullOrBlank
for null or empty or consists solely of whitespace characters.
if (userEmail.isNullOrEmpty())
...
if (userEmail.isNullOrBlank())