1

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?

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
GlCo
  • 11
  • 1
  • Where, during the construction of `Tutorial_selectController`, do you initialize the goBoardController field with a `GoBoardController` instance? – scottb Nov 21 '16 at 00:56
  • I've added it to the initialize() method in Tutorial_selectController after posting this. goBoardController = new GoBoardController();. Still having the same issue though. – GlCo Nov 21 '16 at 01:10
  • So, let me see if I understand this right. Your tutorial instance needs to call a method on your board instance. When you initialize the tutorial, you are not passing a reference to your board instance to it (the one that has the state for the board), instead you are creating a new and unrelated instance and initializing your tutorial instance with that? – scottb Nov 21 '16 at 02:36
  • After reading your comment and looking at my code, it appears that way. When the tutorial is initialized (in this case it would be the StonePlaceTutorial() method) it is updating the scene using the lessonScene Scene object. The lessonScene is created with the GoBoard.fxml which is associated with the GoBoardController. I haven't worked much with fxml until a couple weeks ago, so I thought simply calling the setPiece() via a GoBoardController object would work. – GlCo Nov 21 '16 at 03:25
  • I think this is helpful for you [Passing parameters to another controller](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/40193164#40193164) – CTN Nov 21 '16 at 03:40

0 Answers0