Blank instead of new Line

Flippper368 used Ask the Experts™
I've got a TextField, and now I want, that when a user presses return, a blank is made instead, because my program will crash when there is a return in textfield.getText().
Is this possible?

Thanx for all answers
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Do getText().trim()

I am curious at to why your program crashes when there is a return in getText()??

Distinguished Expert 2018

 This is not normal behaviour... Can you post some of your code where it crashes?
when user presses enter on a text field, if it is empty, it will return null. If you do any String operation on null, it will throw an exception and possibly crash if you have not taken car of exceptions. To avoid that, do a check before calling on any method. if you want to return a blank to the field you can do this:

String t = tField.getText() == null ? " " : tField.getText();


String t = tField.getText();

if( t == null ) t = " ";

after you have made sure the String is not null, you can call on any method for the string.

t = t.trim();
char c = t.charAt(0);
and so on...

you can also do a try{ // code here } catch(Exception e){ // how do you want to handle the exception }

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial