0

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.

4
  • when i am passing the variable to the UserController Class i have a null value How do you know that the value is null? Commented Oct 17, 2019 at 23:12
  • @Abra I edit my code, look the last line when i print in console System.out.println(gg); Commented Oct 17, 2019 at 23:21
  • minimal reproducible example please ... Commented Oct 18, 2019 at 5:36
  • Why are you using ClassName.this.memberName this often? Unless you declare members of the same name in a non-static inner class, this is completely unnecessary and could be replaced by memberName or this.memberName depending on the type containing the member... Commented Oct 18, 2019 at 14:36

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

If C.Sistemas makes a habit of this,he'll often end up with a maintenance nightmare. The potential for a memory leak is there for sure, but in most cases the thing that is much worse that there is no control of who accesses the data. Even if you use package private visibility this can be bad. Also there can only ever be one instance of the field in a jvm which can be problematic for testing. Also I don't recommend doing static imports: Import the containing type instead and use ClassName.memberName. This may increase the code size a bit, but it remains much clearer where the member belongs.
hello @fabian, how i do the containing import? an example?
@CoordinadorSistemas e.g. import java.util.Collections; ... Collections.emptyList(); You could do import static java.util.Collections.emptyList; ... emptyList() instead, but this makes it much harder to tell, where the impelementation recides; The first assumption would be to look for it in the type containing the code, then in enclosing class(es)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.