Javafx 引用位于另一个控制器中的对象时出错;原因:java.lang.NullPointerException“;

Javafx 引用位于另一个控制器中的对象时出错;原因:java.lang.NullPointerException“;,javafx,controller,Javafx,Controller,我想要的是,从第二个控制器(ControllerScreen1)可以修改在另一个控制器(ControllerDocument)中找到的按钮的文本。非常感谢你。以下是我正在使用的代码: package sample; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(ge

我想要的是,从第二个控制器(ControllerScreen1)可以修改在另一个控制器(ControllerDocument)中找到的按钮的文本。非常感谢你。以下是我正在使用的代码:

package sample;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource( "/sample/multiple_fxml_loading/FXMLDocument.fxml" ));
    primaryStage.setTitle("Test");
    primaryStage.setScene(new Scene(root, 600, 500));
    primaryStage.show();
}
public static void main(String[] args) {
    launch(args);
}
}
这是主fxml FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>  

<AnchorPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.multiple_fxml_loading.ControllerDocument">
   <children>
      <BorderPane fx:id="mainPane" >
         <left>
            <VBox alignment="TOP_CENTER" style="-fx-background-color: #AAABBB;" BorderPane.alignment="TOP_CENTER">
               <children>
                  <Button fx:id="button1" mnemonicParsing="false" onAction="#handleButton1" text="Button1">
                     <VBox.margin>
                        <Insets top="20.0" />
                     </VBox.margin>
                  </Button>
                  <Button fx:id="button2"  mnemonicParsing="false" onAction="#handleButton2" text="Button2" />
               </children>
               <BorderPane.margin>
                  <Insets bottom="10.0" left="10.0" top="10.0" />
               </BorderPane.margin>
            </VBox>
         </left>
         <center>
            <Pane fx:id="pane" BorderPane.alignment="CENTER" />
         </center>
         <bottom>
            <TextField fx:id="textFieldMenuContec" text="Menú Contectual" BorderPane.alignment="CENTER" />
         </bottom>
      </BorderPane>
   </children>
</AnchorPane>
第二个fxml文件,screen1.fxml

<?xml version="1.0" encoding="UTF-8"?>

<AnchorPane style="-fx-background-color: #FF5733;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.multiple_fxml_loading.ControllerScreen1">
   <children>
      <Label fx:id="labelPantalla" text="Pantalla 1" />
      <Button fx:id="button" mnemonicParsing="false" onAction="#handleButton" text="Button" />
      <JFXTextField fx:id=""  />
   </children>
</AnchorPane>

您的控制器:

package sample.multiple_fxml_loading;
   
public class ControllerDocument implements Initializable {
    @FXML private BorderPane mainPane;    
    @FXML public Button button1, button2;    
    private static Pane paneView1 = null;
    @FXML void handleButton1(ActionEvent event) {

        System.out.println( "Clic en el Boton 1" );
            ControllerMultipleDocument controllerMultipleDocument = new ControllerMultipleDocument();
            Pane paneView = controllerMultipleDocument.getPage( "screen1" );
            paneView1 = controllerMultipleDocument.getPage( "screen1" );
            mainPane.setCenter( paneView );
            variableBooleana = false;
    }
    public void initialize(URL location, ResourceBundle resources) {
    }       
}
package sample.multiple_fxml_loading;
   
public class ControllerScreen1<JFXTextField> {    
    @FXML private Label labelPantalla;   
    @FXML private Button button;    
    @FXML private JFXTextField textField;    
    @FXML private ControllerDocument controllerDocument;

    @FXML
    void handleButton(ActionEvent event) {
        System.out.println( "Clic : " + labelPantalla.getText() );
        controllerDocument.button1.setText("XXX");    
    }
}
package sample.multiple\u fxml\u加载;
公共类控制器screen1{
@FXML自有标签labelPantalla;
@FXML专用按钮;
@FXML私有JFXTextField文本字段;
@FXML private ControllerDocument ControllerDocument;
@FXML
无效把手按钮(ActionEvent事件){
System.out.println(“Clic:+labelPantalla.getText());
controllerDocument.button1.setText(“XXX”);
}
}
这是让我感到困惑的错误:


线程“JavaFX应用程序线程”java.lang.RuntimeException中的异常:java.lang.reflect.InvocationTargetException。。。48更多原因:java.lang.NullPointerException at sample.multiple_fxml_load.ControllerScreen1.handleButton(ControllerScreen1.java:18)。。。58更多

这是否回答了您的问题?必须加载控制器(相对于实例化)才能注入字段。无关:java命名约定