0

I am trying to pass data between controllers of differen FXML scenes. But I am not able to pass data to another controller. This is my first controller.

public class FXMLDocumentController {

Session session;

private static Stage myStage;

@FXML
private Button button;

@FXML
private TextField txtAd;

@FXML
private TextField txtSifre;

@FXML
void handleButtonAction(ActionEvent event) {

    session = NewHibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();

        Query query = session.createQuery("from Userss where ad = :code and sifre = :sifre");
        query.setParameter("code", txtAd.getText().trim());
        query.setParameter("sifre", txtSifre.getText().trim());
        if (query.list().size() == 1) {

            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setTitle("Giriş");
            alert.setHeaderText(null);
            String s = "This is an example of JavaFX 8 Dialogs... ";
            alert.setContentText(s);
            alert.showAndWait();

        }


        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {

        try {
            session.close();

            FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDevam.fxml"));
            DevamController devamController = loader.getController();
            devamController.as("aaaaaaaaaaaaaaaaaaaaa");
            AnchorPane login = (AnchorPane) loader.load();
            Scene scene = new Scene(login);

            myStage.setTitle("asd");
            myStage.setScene(scene);

        } catch (IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

static void setStage(Stage stage) {
    myStage = stage;
}

}

And this is the second controller.

 public class DevamController implements Initializable {

String as ;


@FXML
private Label lblAd;

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO

    lblAd.setText(as);
}    

public void as (String a){
   as = a;
}

}

I get NullPointerException at this line:

 devamController.as("aaaaaaaaaaaaaaaaaaaaa");

I have tried this and it is not working : JavaFX: passing data between Controllers is always null

Community
  • 1
  • 1
Erdel
  • 370
  • 3
  • 16

1 Answers1

2

The controller is loaded from the FXML file (since that is where it is defined, with fx:controller), so it will be null until you load the FXML with loader.load(). Just switch the order:

        FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDevam.fxml"));
        AnchorPane login = (AnchorPane) loader.load();
        DevamController devamController = loader.getController();
        devamController.as("aaaaaaaaaaaaaaaaaaaaa");

You will need to update the label in your as() method, since initialize() will already have been executed.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • I have already seen you answer in the other link in comments. But I couldnt understand. I m trying to do this for hours. It worked now. Thanks alot. – Erdel Dec 06 '15 at 22:24