あるクラスから別のクラスにStringsを渡そうとしているので、ユーザー名に応じてラベルを初期化できます。Java - あるクラスから別のクラスに値を渡す
ここにありますが、私のコードです:
while (rs.next()) {
uID = rs.getString("ID");
uLoginName = rs.getString("user_login");
uNiceName = rs.getString("user_nicename");
password = rs.getString("user_pass");
if (uLoginName.equals(username) && password.equals(enteredPass)) {
***//Values That I am Trying to pass***
TutorControlPanelController panel = new TutorControlPanelController();
panel.setUserLoginName(uLoginName);
try {
Parent root = FXMLLoader.load(getClass().getResource("SomePath"));
Scene scene = new Scene(root);
String css = this.getClass().getResource("Style.css").toExternalForm();
scene.getStylesheets().add(css);
Stage stage = new Stage();
stage.setTitle("Tutor Control Panel");
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
System.out.println("user not found");
}
}
} catch (SQLException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
}
他のクラスでは、私は
public class SomeClass implements Initializable {
@FXML
private GridPane gridView;
@FXML
private Button recordButton;
@FXML
private Button stopButton;
@FXML
private MenuButton settingsButton;
@FXML
private MenuItem qualitySettings;
@FXML
private MenuItem videoSettings;
@FXML
private Button lessoncategory;
@FXML
private MenuButton serverUpload;
@FXML
private MenuItem startUpload;
@FXML
private MenuItem stopUpload;
@FXML
private Button timer;
@FXML
private Label timerHours;
@FXML
private Label timerMinutes;
@FXML
private Label timerSeconds;
@FXML
private Button logOut;
@FXML
private Label uNameLabel;
private String uID;
private String username;
private String userLoginName;
private String userNiceName;
public TutorControlPanelController(String uID, String username, String userNiceName) {
this.uID = uID;
this.username = username;
this.userNiceName = userNiceName;
}
/**
* Initializes the controller class.
*/
public TutorControlPanelController() {
}
public String getuID() {
return uID;
}
public void setuID(String uID) {
this.uID = uID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserNiceName() {
return userNiceName;
}
public void setUserNiceName(String userNiceName) {
this.userNiceName = userNiceName;
}
public String getUserLoginName() {
return userLoginName;
}
public void setUserLoginName(String userLoginName) {
this.userLoginName = userLoginName;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
assert uNameLabel != null : "fx:id=\"someID\" was not injected: check your FXML file 'somePath'.";
setlabels();
}
public void setlabels() {
uNameLabel.setText(getUserLoginName());
// and just to check also printing the value
System.out.println(getUserLoginName());
}
私は取得していますすべてがnull
更新やって 私はそれは難しい正確に何が起こっているかを把握するために見つけていますに。
ユーザーログイン名の値を取得して設定する両方のクラスのコードを投稿できますか? –
あなたは今すぐ仲間を確認してください –
setUserLoginName()の後にsetlabels()が呼び出されていることは確かですか?デフォルトのコンストラクタがあり、setuserLoginNameが必ず呼び出されるという特別な要件はないので、インスタンス変数を空のStringに初期化してNPEの問題を回避します。可能であれば、デフォルトコンストラクタを削除してください。 – KevinO