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 wil
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
try in this way
String edittext = edittext.getText().toString();
if(edittext.length==0){ Log.d("null","the valueis 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();
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()
.
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.
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!