I have a few edittexts and a button.
On click of the button, I do some action with edittext values. I wish to check if all edittexts are filled in before I proceed. So I use code in onclick listener
if("".equals(etLandSize.getText().toString()) || null == etLandSize.getText().toString())
{
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return;
}
//followed by some actions.
If the above condition goes true, I expect the method to return without doing anything by using a ‘return’.
My problem is that even when the condition is going true, the lines following this condition are executing and I see the NPEs from log since the fields are empty.
If I comment all the lines after this condition, only then I see the expected Toast and my Activity remains there, which is normal behavior.
Am I doing something wrong here?
An alternate is that i can put the code following condition in else{} block. But just wondering why I cannot use a return. Apologies if I’m not realising some basic mistake.
Your condition looks weird.
I’m pretty sure
null == etLandSize.getText().toString()will never be true, and I would useinstead of
"".equals(etLandSize.getText().toString(), since using""to avoid comparisson withnullis not usefull here – toString() cannot returnnull.So try this:
BTW: if your
return;is withintry {}block, then part of the coded located infinally{}block will be run after it.