Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does EditText.getText() ever returns null?

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?

like image 388
AppleGrew Avatar asked Oct 25 '13 12:10

AppleGrew


People also ask

Can getText return null?

getText() ever return null? @tsp your code will result in NPE in that case. Maybe the warning is really saying "the user might have typed the word, 'null'" into the edittext.

What does getText return?

it actually returns Editable and not CharSequence but you can store it in a String variable by calling toString() on it.

What does an empty EditText return?

It will return true if its empty/null.


1 Answers

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(); 
like image 138
stinepike Avatar answered Sep 22 '22 18:09

stinepike