Does Edittext.Gettext() Ever Returns Null

Does EditText.getText() ever returns null?

getText() will not return null. So there is no chance for NPE in following method. the getText will return empty string if there is no string, which is definitely not null

getText().toString();

However the edittext itself can be null if not initialized properly, Hence the following will trigger NPE

editText.getText().toString();

Does TextView.getText() ever returns null?

NO. textView.getText() will never return null. By definition getText() is as follows .

public CharSequence getText() {
return mText;
}

And you can see the instance variable mText initialized with mText="" just inside the constructor of TextView . You can have a look at source code of TextView.

public TextView(
Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);

mText = "";
.........
}

So it will either return text and "" if there is not text.

Can EditText.getText() ever be null?

Editable is an interface. It is possible that the implementations of this interface that you use cannot be null, but even if we factually knew that it is safe in your use-cases to not handle NPE, I would advise against it. There might be implementations for that interface in use that might have null as a value. You could implement an util method to solve this if the problem is that you repeat this over and over again.

EDIT

As Ryan M has pointed out in the comment section and the documentation confirms, Editable does not have a getText method, however, this does not change the general thoughts of separating our concerns.

What does an EditText.getText() in android return if it is empty?

No other possibility.

getText, infact, will never return null. It returns CharSequence whose contents may be empty.

Instead of doing getText().toString().equals("") or vice-versa, it may be faster to do getText().length() == 0

getText() from EditText: is it possible to get a 'null'?

I think the lint check is telling you something slightly differently:

String value = input.getText().toString();
if (value == null || value.length() == 0) {

Condition 'value == null' is always false.

It actually doesn't matter in this if-statement whether or not EditText.getText() can ever return null. Because if it returns null, then the .toString() will have caused a NullPointerException before the if-statement is even reached. Therefore, if the if-statement is at all reachable, value cannot be null.

You can dig all through the Android source code, and maybe you will find that there's no possible way for EditText to return null. If that is the case, then you can just ignore the lint warning if you know that the standard EditText is in your app.

However, in the more general case, nothing is stopping me from sticking this in the view hierarchy:

public class EvilEditText extends EditText {
// Constructors

@Override
public Editable getText() {
return null;
}
}

so the safest option is to rewrite your code to be completely nullsafe:

Editable e = input.getText();
String value = (e == null ? null : e.toString());
if (TextUtils.isEmpty(value)) { //hat tip to Deepak Goyal's answer. Could also use isEmpty(e)

Android getText from EditText return NULL

Please move this

final String Textusername = username.getText().toString();
final String Textpassword = password.getText().toString();

inside onClick().

What does getText() return for an empty EditText?

A summary of my previous comments:

String text = gateNumberEditText.getText().toString();
if (text.equals("")){

\\do something
}else {
\\do something else
}

If you had a null, this code would have thrown a NullPointerException on the toString() method call.

Seeing as getText() returns a String, there is no reason to call toString()

When you want to compare values of String objects, always use the equals(IgnoreCase)() method.

Also, remember, " " (a space) and "" are not two identical values, so comparing them will indeed return false.
Change your code to this:

String text = gateNumberEditText.getText().trim();
// the trim() method will remove all leading and trailing spaces
if (text.isEmpty()){ // this method of the String class, will check the length of the String.
// after the trim(), for an empty String (or only spaces), that would be zero
\\do something
}else {
\\do something else
}

Why are all my EditText.getText().toString() returning an empty String?

The problem is you initialise your Strings in your OnCreate function. This function is called only once, when your activity starts; therefore, at this point your EditTexts are all empty without any text in it.

To get the current text located in your EditTexts, you should move your initialisations in your goToRegisterAs() function.

    void goToRegisterAs(View view) {
name = nameEditText.getText().toString();
email = emailEditText.getText().toString();
password = passwordEditText.getText().toString();
city = cityEditText.getText().toString();
country = countryEditText.getText().toString();
if (!(name.equals("") || email.equals("") || password.equals("") ||
city.equals("") || country.equals(""))) {
if (email.contains("@") && email.contains(".")) {
if (MainActivity.database.donorsDao().checkEmail(email) == 0 &&
MainActivity.database.plantersDao().checkEmail(email) == 0) {
Intent intent = new Intent(view.getContext(), RegisterAsActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("password", password);
intent.putExtra("city", city);
intent.putExtra("country", country);

view.getContext().startActivity(intent);
} else {
alreadyAccountTextView.setText("Already registered. Please log in.");
}
} else {
alreadyAccountTextView.setText("Please enter correct email address.");
}
} else {
alreadyAccountTextView.setText("Please enter all fields");
alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country);
}
}


Related Topics



Leave a reply



Submit