我正在尝试将一个字符串从一个类传递到另一个类,因此我可以根据用户名初始化标签。
这是我的代码:
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());
}
我得到的只是空 我发现很难弄清究到底发生了什么。
答案 0 :(得分:0)
我不知道你在第二堂课的第一堂课叫哪里。 你必须调用第一个作为seconde的属性作为例子,然后你才能获得userLoginName。
这是你在第二堂课中可以做的事情:
Instr()
答案 1 :(得分:0)
目前,类TutorControlPanelController
有一个默认构造函数和一个参数化构造函数。调用默认构造函数时,String实例变量全部初始化为null。他们可以通过调用各种方法来设置,但不能保证它们会成为。
更好的方法是删除默认构造函数(删除此代码):
public TutorControlPanelController() {
}
在带有rs.next()的代码片段中,更改调用以使用参数化构造函数:
if (uLoginName.equals(username) && password.equals(enteredPass)) {
TutorControlPanelController panel = new TutorControlPanelController(uId, uLoginName, uNiceName);
try {
...
这种方法可以确保大多数实例变量都已初始化。
然而,因为这里代码不足,我不知道默认构造函数是否有框架要求。如果是,则初始化String实例变量以使其具有空String
private String uID = "";
private String username = "";
private String userLoginName = "";
private String userNiceName = "";
在这种情况下,您将避免使用NPE。