Coordinador Sistemas
Coordinador Sistemas

Reputation: 19

Null data Passing Variable from Controller A to Controller B

Hello I am Trying to pass Variable data from database from one Controller to another, but I Have one problem, when I am passing the data to the controller A to Controller B, appear a Null value, the jerarchy between Scenes is this:

LoginController->AdminController->UserController

in the LoginController to Access AdminController and Pass the Value i do this:

    public String getRoladmin() {
    return roladmin;
}


    public void setRoladmin(String roladmin) {
    this.roladmin = roladmin;
}


   public String roladmin="";

I Capture the Variable roladmin in this portion of my code like this:

 while(rs.next()) {

                 comparauser=rs.getString("NOMBREUSUARIO");
                 comparapass=rs.getString("PASS");
                 miadmin=rs.getString("NOMBRE_ADMIN");
                 roladmin=rs.getString("ROL_ADMIN");


             }

to access the Stage I validate if the user and pass are correct like this:

 ----rest of code---   if(comparauser.equals(fusuario.getText().toString())&&comparapass.equals(fcontrasena.getText().toString().trim())) {

                     try {
                            Stage administrador=new Stage();
                            FXMLLoader carga = new FXMLLoader(getClass().getResource("Admin.fxml"));
                            Parent StackPane = (Parent)carga.load();
                            Scene scene = new Scene(StackPane);

                            AdminScreenController loadl = carga.<AdminScreenController>getController();
                            /*loadl.UserScreen.setText("Bienvenido"+"  "+fusuario.getText().toString());*/
                            loadl.UserScreen.setText("Bienvenido"+"  "+miadmin);
                            /* String r=loadl.UserScreen.getText().toString();*/





      -----in this part I call the LoginController and pass the variable Roladmin------



----begin of call--
                                String h=LoginController.this.roladmin;
                                LoginController.this.setRoladmin(h);
                                String r=LoginController.this.getRoladmin();
                                loadl.setCapdata(r);



    -----end of call-----

                                if(!r.equals("ADMINISTRADOR")) {

                                        loadl.btnimg5.setDisable(true);
                                        loadl.btnimg5.setStyle("-fx-opacity:0.65;");

                                    }
                                else {
                                    loadl.btnimg5.setDisable(false);
                                }

                                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                                administrador.setScene(scene);
                                administrador.setTitle("AdminScreen");
                                Stage login=(Stage)fusuario.getScene().getWindow();
                                login.hide();
                                administrador.show();
                         }catch(Exception e) {
                             Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, e);
                         }
                     }
    ---rest of code---  

now i Pass that variable to the AdminController like this

in AdminController i have this:

public String capdata;
    public String h;

    public String getCapdata() {
        return capdata;
    }

    public String setCapdata(String data) {
        return this.capdata = data;
    }


this is the code i have to load UserController Stage:


    public void UserView() throws IOException {
        Stage userstage = new Stage();
        FXMLLoader cargauser =new FXMLLoader(getClass().getResource("UsuarioScreen.fxml"));
        Parent StackPane = (Parent)cargauser.load();
        UserController cargatodouser = cargauser.<UserController>getController();
        String pasadato=UserScreen.getText().toString();
        cargatodouser.iduser.setText(pasadato);

  ---begin of call to pass the variable---          

h=AdminScreenController.this.getCapdata();

---end of call---   



        /*String r=cargatodouser.iduser.getText().toString();*/
        Scene scene = new Scene(StackPane);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        userstage.setScene(scene);
        userstage.setTitle("QuoraApp");
        Stage AdminScreen=(Stage)btnimg1.getScene().getWindow();
        AdminScreen.hide();
        userstage.show();
}

now when i am passing the variable to the UserController Class i have a null value I am doing this:

In UserController class i have this to go back AdminController:




public String capturau;



public String getCapturau() {
    return capturau;
}

public String setCapturau(String capturau) {
    return this.capturau = capturau;
}

        public void inicio() throws IOException {

                            Stage administrador=new Stage();
                            FXMLLoader carga = new FXMLLoader(getClass().getResource("Admin.fxml"));
                            Parent StackPane =(Parent) carga.load();

                            AdminScreenController loadl = carga.<AdminScreenController>getController();
                            String pasadato=iduser.getText().toString();
                            loadl.UserScreen.setText(pasadato);

                            /*Captura.setText(loadl.getCapdata());
                            String captura=Captura.getText().toString();
                            System.out.println(captura);*/


                           UserController.this.setCapturau(loadl.getCapdata());
                           String gg=UserController.this.getCapturau();
                           System.out.println(gg);

        }

what i am doing wrong? a little help here please.

Upvotes: 0

Views: 60

Answers (1)

Vector
Vector

Reputation: 3235

You need to look into the concept of static variables
Here is a static variable I declare on one Controller and use and another Controller
A BIG word of caution about using static variables or Global Variables
What ever you put in a static variable it holds that value till you clear it or change it

static Integer strID;

Here is the use of the static variable strID in the other Controller Class
Notice it needs to be imported to the new Controller

    import static diary.ChildTableViewController.strID;

private void ReadDetailView() throws SQLException{

    stmnt = conn.createStatement();
    ///String sql = "SELECT * FROM Child WHERE CID = "+strID;
    ///ResultSet rs = stmnt.executeQuery(sql);
    ResultSet rs = stmnt.executeQuery("SELECT * FROM Child WHERE CID = "+strID);

Welcome to SO

Upvotes: 1

Related Questions