Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

empty textfield value

  Asked By: Francisca    Date: Dec 02    Category: Java    Views: 1334
  

What is the value returned by surname.getText()
of a textfield, if someone doesn't enter any value?
How do you test for it?<br><br>I tried:<br>if
(surname.getText() == "")<br>and<br>if (surname.getText() ==
null)<br>but neither seems to work - or am I doing something
else wrong

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Nixie Schmidt     Answered On: Dec 02

Try using the equals() method like so:<br><br>if
(surname.getText().trim().equals(""))

 
Answer #2    Answered By: Nagaraju Iyaner     Answered On: Dec 02

If surname has not been initialized it will be
null. You will experience a NullPointerException
whenever you try to call one of surname's methods in this
situation. <br><br>Test first for the null.<br>if (surname
!= null) {// do work  here}

 
Didn't find what you were looking for? Find more on empty textfield value Or get search suggestion and latest updates.




Tagged: