0

I am unable to access from another class, I have created two pane with different controllers in one FXML File, second pane has to move around the first pane, on Click Action, and I have loaded an FXML file in second pane, also successfully moved the pane one time, but the second time I want to move it from second controller, but it is giving me an NullPointerException.

this is my main controller where I have moved the pane:

public class MainController implements Initializable {

@FXML
private Pane searchPane;
@FXML
private Pane secondPane; 

private TranslateTransition nextTransition;

public Pane getSecondPane() {
    return secondPane; // Accessing Pane with this getter method
}

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

}    

@FXML
private void nextBtnAction(ActionEvent event) {
    try {
        Parent pessFxml =  FXMLLoader.load(getClass().getResource("firstPane.fxml"));
        secondPane.getChildren().add(pessFxml);
        nextTransition = new TranslateTransition(Duration.millis(300), secondPane);
        nextTransition.setToX(searchPane.getLayoutX()-secondPane.getLayoutX());
        nextTransition.play();
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Form does not found" ,ex.toString(),0);
    }
}

}

this is SecondController where I am accessing the pane to move it back smoothly, but its throwing NullPointerException: please tell me how to solve this

public class SecondController implements Initializable {

MainController mainControll;


/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

}

@FXML
private void backBtnPessAction(ActionEvent event) {
    //here i am putting the second pane to move back
    TranslateTransition back = new TranslateTransition(Duration.millis(300), mainControll.getSecondPane());
    back.setToX(mainControll.getSecondPane().getLayoutX()-750);
    back.play();
}

}

user7291698
  • 1,972
  • 2
  • 15
  • 30

1 Answers1

0

MainController is never initialized in SecondController. You should pass it when creating second controller in nextBtnAction.

MainController code:

private void nextBtnAction(ActionEvent event) {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("firstPane.fxml"));
        Parent pessFxml =  loader.load();
        SecondController controller = (SecondController)loader.getController();
        controller.setMainController(this);
        secondPane.getChildren().add(pessFxml);
        nextTransition = new TranslateTransition(Duration.millis(300), secondPane);
        nextTransition.setToX(searchPane.getLayoutX()-secondPane.getLayoutX());
        nextTransition.play();
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Form does not found" ,ex.toString(),0);
    }
}

SecondController:

public void setMainController(MainController controller) {
    this.mainControll = controller;
}
MBec
  • 2,172
  • 1
  • 12
  • 15