I am trying to access a method from one controller class inside a separate controller. The method I'm trying to access, setPiece()
is used to draw an Image onto a pane and is located in the controller GoBoardController
. The controller I'm attempting to access this method from is Tutorial_selectController
.
Within the Tutorial_selectController, I'm calling the setPiece()
method in the StonePlaceTutorial() method. I thought calling the setPiece()
method through a GoBoardController object would work, however I get a RuntimeException
and InvocationTargetException
whenever the setPiece()
method is called inside the controller.
GoBoardController.java
public class GoBoardController implements Initializable {
@FXML
private ImageView goBoardImageView;
@FXML
private Button boardBackButton;
@FXML
private Button boardBackTutorial;
@FXML
private AnchorPane boardPane;
private GoBoard goBoard;
FXMLLoader loader;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
//Creating a new GoBoard object on initilization of the controller
goBoard = new GoBoard(boardPane);
}
//Method I'm trying to access
public void setPiece(double x, double y, String p){
if(p == "b"){
goBoard.setStoneBlack(x, y);
}
else if(p == "w"){
goBoard.setStoneWhite(x, y);
}
}
}
Tutorial_selectController.java
public class Tutorial_selectController implements Initializable {
@FXML
private Button quitButton;
@FXML
private Button mainMenuButton;
@FXML
private Button stonePlaceButton;
private Go_Tutorial_Lessons lessons;
private Stage lessonStage;
private Parent root;
private Scene lessonScene;
private GoBoardController goBoardController;
//x and y values will be used to pre-set the board for specific tutorials
private final int x1 = 349;
private final int y1 = 99;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
lessons = new Go_Tutorial_Lessons();
try{
root = FXMLLoader.load(Go.class.getResource("/GoFXML/GoBoard.fxml"));
}catch(Exception e){
e.printStackTrace();
}
lessonScene = new Scene(root);
}
//Where I'm attempting to access the setPiece() method
@FXML
private void StonePlaceTutorial(ActionEvent event) throws IOException {
lessonStage = (Stage)mainMenuButton.getScene().getWindow();
lessonStage.setScene(lessonScene);
goBoardController.setPiece(x1, y1, "b"); //doesn't work, why.
lessons.L1_Stone_Placement();
}
}
How can I access the setPiece()
method correctly so it can draw what it needs to on the screen?