Does EditText.getText() ever returns null?
AndroidAndroid EdittextAndroid Problem Overview
All over the net I see examples like edittext.getText().toString()
. I do not see any null check. In docs I do not see any statement that would say that this will never be null.
Still, what does the observations say; does this ever return null?
Android Solutions
Solution 1 - Android
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();
Solution 2 - Android
UPDATE:
It does appear as of Jan 18, 2018 this is now possible.
OLD ANSWER:
No, EditText.getText()
never returns null
. One way to verify this is to check the Android source code for EditText.getText()
:
EditText.java shows:
public Editable getText() {
return (Editable) super.getText();
}
Since EditText extends TextView
, the call to super.getText()
must be TextView.getText()
. Now we move on to TextView.getText()
to see what it returns:
TextView.java shows:
public CharSequence getText() {
return mText;
}
Now we need to know if mText
could ever be null.
Digging deeper into the TextView.java source we see that mText
is initialized as an empty string in the TextView
constructor:
public TextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mText = "";
…
}
Once we see that the EditText
constructor calls the TextView
constructor:
public EditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
we can safely conclude that EditText.getText()
can never return null
, because as soon as an EditText
is constructed, mText
is given a value of an empty string.
However, as StinePike pointed out, EditText.getText()
can possibly cause an NPE if your EditText is null
when it makes the call to getText()
.
Solution 3 - Android
With Android P SDK it is annotated as nullable in the AppCompatEditText
class so it can return null.
And from the docs: > Return the text that the view is displaying. If an editable text has not been set yet, this will return null.
@Override
@Nullable
public Editable getText() {
if (Build.VERSION.SDK_INT >= 28) {
return super.getText();
}
// A bug pre-P makes getText() crash if called before the first setText due to a cast, so
// retrieve the editable text.
return super.getEditableText();
}
Solution 4 - Android
I dont think so it will ever return null
.
But if you want to check whether the returned text is empty or not might I suggest using TextUtils.isEmpty()
method
Edit:- The documentation doesn't states anything regarding the returned value. And from what I've seen in the source code is that when you initialize a EditText, the default text value is set to ""
. So it will never return null
Solution 5 - Android
it will return null because when apps runs its empty and it returns null, use .getText.toString inside a button click listener, now when you click button it will get the text which you have entered on editText.
Solution 6 - Android
it is just because your edittext.getText().toString()
is called directly in onCreate()
...
This is why it return the initial value of editText.
You just have to create a function out of onCreate()
and call it in event listener.
onCreate (){
….............
btn = (Button)findViewById(R.id.firstButton);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
getValue ()
}
});
}
public void getValue () {
String editTextValue= edittext.getText().toString();
......
}
Or you Can call it directly in you onClickListenner
in onCreate()
.
Like this:
onCreate (){
….............
btn = (Button)findViewById(R.id.firstButton);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String editTextValue= edittext.getText().toString();
.....
}
});
}
I Hope it will be helpful!
Solution 7 - Android
try in this way
String edittext = edittext.getText().toString();
if(edittext.length==0){ Log.d("null","the valueis null")};