简体   繁体   English

Java-将值从一类传递到另一类

[英]Java - Passing values from one class to another

I am trying to pass a Strings from one class to another so I can initialize the labels according to the usernames. 我正在尝试将Strings从一个类传递到另一个类,以便可以根据用户名初始化标签。

Here's is my code: 这是我的代码:

    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);
            }

in other Class , I am doing UPDATED 在其他班级,我在做UPDATED

      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());
    }

All I am getting is null I am finding it hard to figure out what exactly is going on. 我得到的只是空的,我发现很难弄清楚到底发生了什么。

I dont see where do you call your first class in your second one. 我看不出您在第二堂课上怎么称呼第一堂课。 You must call the first one as an attribute to the seconde one for exemple, then you'll be able to get the userLoginName. 例如,您必须将第一个属性作为第二个属性的属性,然后才能获得userLoginName。

This is what you can do in your second class : 这是您在第二堂课中可以做的:

FirstClass myFirstClass = new FirstClass();
uNameLabel.setText(myFirstClass.getUserLoginName());

At the moment, the class TutorControlPanelController has a default constructor and a parameterized constructor. 目前,类TutorControlPanelController具有默认构造函数和参数化构造函数。 When the default constructor is invoked, the String instance variables are all initialized to null. 调用默认构造函数时,所有String实例变量均初始化为null。 They may be set by calling the various methods, but there is no guarantee they will be. 可以通过调用各种方法来设置它们,但是并不能保证会如此。

The better approach would be to remove the default constructor (delete this code): 更好的方法是删除默认构造函数(删除此代码):

public TutorControlPanelController() {

}

And in the code snippet with the rs.next(), change the call to use the parameterized constructor: 然后在带有rs.next()的代码段中,将调用更改为使用参数化的构造函数:

if (uLoginName.equals(username) && password.equals(enteredPass)) {
  TutorControlPanelController  panel  = new TutorControlPanelController(uId, uLoginName, uNiceName);

  try {
   ...

This approach would ensure that most of the instance variables were initialized. 这种方法将确保大多数实例变量都已初始化。

However , as there is insufficient code here, I do not know if there is a framework requirement for the default constructor. 但是 ,由于这里的代码不足,因此我不知道默认构造函数是否有框架要求。 If so, then initialize the String instance variables to have an empty String 如果是这样,则将String实例变量初始化为具有空String

private String uID = "";
private String username = "";
private String userLoginName = "";
private String userNiceName = "";

You will avoid NPEs in this case. 在这种情况下,您将避免使用NPE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM