Android Check Null or Empty String in Android

Android check null or empty string in Android

From @Jon Skeet comment, really the String value is "null". Following code solved it

if (userEmail != null && !userEmail.isEmpty() && !userEmail.equals("null")) 

how to check if a string is empty or null in Android Studio

Just Use

if(TextUtils.isEmpty(/* your String*/)){
// String is empty or null
}else {
// string has value
}

How to check a string that not empty in java?

You can see if the characters match

if (str.equals ("")) {
// true
}

if (str3.equals (" ")) {
// true
}

Check the null values in android edit text

You can check null values from the both by following code :

Signbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

final ProgressDialog mdialog = new ProgressDialog(signin.this);
mdialog.setMessage("please waiting...");

if (!TextUtils.isEmpty(editphone.getText.toString.trim()) && !TextUtils.isEmpty(editpassword.getText.toString.trim())) {

table_user.addListenerForSingleValueEvent(new ValueEventListener() {

@Override
public void onDataChange(DataSnapshot dataSnapshot) {

if(dataSnapshot.child(editphone.getText().toString()).exists())
{
mdialog.dismiss();
user userobj = dataSnapshot.child(editphone.getText().toString()).getValue(user.class);

if (userobj.getPassword().equals(editpassword.getText().toString())) {
Toast.makeText(getApplicationContext(), "Sign in successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Sign in failed", Toast.LENGTH_SHORT).show();
}
}
else
{

Toast.makeText(getApplicationContext(), "user is not exist", Toast.LENGTH_SHORT).show();
}

}
}
}

Check if multiple strings are empty or not without using if and when

You can make one generic function to check all string are valid or not.

You can take n number of strings as vararg and can check each of them by making one util or extension function.

Here is Util class

object StringUtils {

fun isAllValid(vararg args: String?) : Boolean {

//checking all strings passed and if a single string is not valid returning false.
args.forEach {
if(isNotValid(it))
return false
}
return true
}

fun isValid(string: String?): Boolean {
return string != null && string.isNotEmpty() && string.isNotBlank()
}

fun isNotValid(string: String?) : Boolean {
return isValid(string).not()
}

}

and you can use like this

fun changeUserAccount(
userName: String,
password: String,
confirmPassword: String,
realName: String,
accountEmail: String,
applicationContext: Context
) {

//You can pass n number of strings.
val isAllStringsValid = StringUtils.isAllValid(userName,password,confirmPassword,realName,accountEmail)

//this returns true
StringUtils.isAllValid("hi","how","are","you","?")


//this returns false
StringUtils.isAllValid("","how","are","you","?")


//this returns false
StringUtils.isAllValid(null,"how","are","you","?")


//this returns false
StringUtils.isAllValid("","how","are","you","?")
}

java.lang.NumberFormatException: empty String in Android

Kindly do below changes, i.e. do trimming of string for space.

String totalOutMekanik = edTotalOutMekanik.getText().toString().trim();
String totalInMekanik = edTotalInMekanik.getText().toString().trim();

If you look official document of the {string}.isEmpty()then you find just looks length of the string and tells wether it has value or not. It does not consider spaces. In your case I guess space was causing problem.

How do I check if my EditText fields are empty?

I did something like this once;

EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return;
}

how to check the string array is empty or null android?

Very precisely

if(k!=null && k.length>0){
System.out.println(k.length);
}else
System.out.println("Array is not initialized or empty");

k!=null would check array is not null. And StringArray#length>0 would return it is not empty[you can also trim before check length which discard white spaces].

Some related popular question -

  • What is null in java?
  • Avoiding “!= null” statements in Java?


Related Topics



Leave a reply



Submit