How to get String from TextField in Java

2.4k Views Asked by At

I am getting object from TextField and PasswordField, but I need to use it as a String in my class. I have variables set like this, but the application is crashing

@FXML private TextField email;
@FXML private PasswordField password;

private String stringPassword = password.getText();
private String stringEmail = email.getText();

Edit: I put the getText to the relevant method, with no success. Now I have

@FXML public void logIn() throws IOException    
{ 
    String p = password.getText();
    String e = email.getText();
    System.out.println(p);}

All this leading to NullPointerException

1

There are 1 best solutions below

2
kamel2005 On

you must not call this password.getText(); and email.getText(); in that place. because TextFieldand PasswordFieldare not initialized yet. So you should call them in a method in your class.