如何从Java中的TextField获取String

时间:2021-03-08 17:03:51

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

我从TextField和PasswordField获取对象,但我需要在我的类中使用它作为String。我有这样的变量设置,但应用程序崩溃了

@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

编辑:我把getText放到相关的方法,没有成功。我现在有

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

All this leading to NullPointerException

所有这些导致NullPointerException

1 个解决方案

#1


3  

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.

你不能调用这个密码.getText();和email.getText();在那个地方。因为TextField和PasswordField尚未初始化。所以你应该在你班上的方法中调用它们。

#1


3  

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.

你不能调用这个密码.getText();和email.getText();在那个地方。因为TextField和PasswordField尚未初始化。所以你应该在你班上的方法中调用它们。