I have a common controller, from which I want to get a link to the controller of the internal element of the scene, quote the class code:

public class RootViewController { private DocumentTableOverviewController documentController; private Stage primaryStage; private BorderPane rootPane; private ObservableList<RailroadDocument> documentList = FXCollections.observableArrayList(); public void initController(){ initRootPanel(); initDocumentPanel(); } private void initDocumentPanel(){ try { FXMLLoader loader = new FXMLLoader(); loader.setLocation(RootViewController.class.getResource("components/DocumentTableOverview.fxml")); AnchorPane docTable = (AnchorPane)loader.load(); rootPane.setCenter(docTable); documentController = loader.getController(); //Вставил задержку, чтобы убедиться что ссылка активна new Thread(new Runnable() { @Override public void run() { try{ Thread.currentThread().sleep(3000); }catch(InterruptedException e){ } System.out.println(documentController);// ссылка активна != null } }).start(); } catch (IOException e) { e.printStackTrace(); } } private void initRootPanel(){ try { FXMLLoader loader = new FXMLLoader(); loader.setLocation(RootViewController.class.getResource("RootView.fxml")); rootPane = (BorderPane)loader.load(); } catch (IOException e) { e.printStackTrace(); } } //Метод привязан к кнопке test приложения @FXML public void testButtonAction(){ System.out.println(documentController);//Ссылка == null } } 

When the testButtonAction() method is called, the reference is zero. I don’t understand why this is the case, because specifically in the initialization method I set sleep for 2 seconds to make sure that the link remains active. It feels like the testButtonAction() method is executed in another application, in which documentController == null;

  • add code where it becomes null - Mikhail Vaysman
  • // The method is tied to the test button of the @FXML application public void testButtonAction () {System.out.println (documentController); // Reference == null} - Yuri Yakhnitsa
  • try adding a minimal reproducible example to the question - Mikhail Vaysman
  • Found an error, the link that the test method uses to an instance of the RootViewController class and an instance of the new RootViewController () class are two different elements. I did not completely understand how class instances are created during initialization of a javaFX application, and this is where the problem arose. - Yuri Yakhnitsa

1 answer 1

Found a mistake the whole snag was in the following: method:

  private void initRootPanel(){ try { FXMLLoader loader = new FXMLLoader(); loader.setLocation(RootViewController.class.getResource("RootView.fxml")); rootPane = (BorderPane)loader.load(); } catch (IOException e) { e.printStackTrace(); } } 

During the execution of the code, a new instance of the RootViewController class is created that is not related to an instance of the class in which the code runs, for example, when creating a new RootViewController () and further initializing the panel using the initRootPanel () method; The system will launch two instances of the RootViewController class, one in the constructor, the other through the FXMLLoader.load () method. getController ();

  • Well done. To make it look like a response, in addition to the description of the error, it was necessary only to add that the solution is to remove this initRootPanel() method from the controller code. - oshatrk