I am trying to get the username after logged in from the text field and pass to another scene by instantiated a new object of the "logincontroller" class, but it shows the "NullPointerException" error. In the first class "Logincontroller" I managed to print the username by calling the method "CurrentUsername()" , the same method is called in another class "AddDocument" but doesn't work . First , is it correct to store the username in from the username text field and access it in another scence ? is there a better way to access username in another scence after logged in ? I wonder, and I would be very gratefull if anyone can help me.
public class LoginController implements Initializable {
@FXML
private TextField txtUsername;
@FXML
private TextField txtPassword;
@FXML
.
.
.
@FXML
public void loginaction (ActionEvent event){
try {
if (this.loginmodel.isLogin(this.txtUsername.getText(),this.txtPassword.getText(),((Option)this.RoleSelection.getValue()).toString())){
Stage stage = (Stage)this.btnLogin.getScene().getWindow();
stage.close();
switch (((Option)this.RoleSelection.getValue()).toString()){
case "Employee" :
EmployeeLogin ();
break;
case "Student":
StudentLogin();
break;
case "Admin":
AdminLogin();
System.out.println(CurrentUsername()); // Here it has value and shows the username
break;
}
}
else {this.loginStatus.setText("Der Benutzername oder das Kenntwort ist nicht Korrekt");}
}
catch (Exception localException){
// System.out.print("test failed");
}
}
public String CurrentUsername() {
String username = txtUsername.getText();
return username;
}
}
public Class AddDocument {
@FXML
private TextField DocumentId;
@FXML
private TextField DocumentName;
@FXML
private TextField Date;
.
.
.
// for the username I created a new object from the class where there is the text field "txtUsername.getText()"
public static void main(String[] args) {
LoginController loginController = new LoginController ();
String User = loginController.CurrentUsername();
System.out.println(User); // here it it doesn't show the value, but an exception Caused by: java.lang.NullPointerException
}
}