我正在尝试向文本字段中添加一些文本,但是当我单击按钮时它显示nullpointerexception,为什么会发生这种情况?

MainWindowController.java

@FXML
public TextField konsumatoriPunetField = new TextField();

@FXML
private void initialize()
{
    FXMLLoader loader5 = new FXMLLoader();
    loader5.setLocation(getClass().getResource("ZgjedhKonsumatorinFXML.fxml"));
    BorderPane border5 = new BorderPane();
    border5 = loader5.load();
    Scene scene5 = new Scene(border5);
    zgjedhkonsumatorinstage.setScene(scene5);
    zgjedhkonsumatorinstage.setTitle("Pit Stop");
    zgjedhkonsumatorinstage.initModality(Modality.WINDOW_MODAL);
    zgjedhkonsumatorinstage.initOwner(MainFXMLController.mainFXMLStage);
}

@FXML
public void zgjedhKonsumatorin()
{
    zgjedhkonsumatorinstage.showAndWait();
}


MainWindowFXML.fxml

<TextField fx:id="konsumatoriPunetField" editable="false" onMouseClicked="#zgjedhKonsumatorin" promptText="Kliko per te zgjedhur" GridPane.columnIndex="1" GridPane.rowIndex="1" />


ZgjedhKonsumatorinController.java

@FXML
public void zgjedhKonsumatorin()
{
    FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindowFXML.fxml"));
    MainWindowController c = (MainWindowController) loader.getController();
    c.konsumatoriPunetField.textProperty().setValue("ertani");
    main.zgjedhkonsumatorinstage.close();

}


ZgjedhKonsumatorinFXML.fxml

<Button mnemonicParsing="false" onAction="#zgjedhKonsumatorin" prefWidth="150.0" text="Zgjedh Konsumatorin" />


输出:

Caused by: java.lang.NullPointerException
    at main.ZgjedhKonsumatorinController.zgjedhKonsumatorin(ZgjedhKonsumatorinController.java:193)
    ... 102 more


ps。这是ZgjedhKonsumatorinController中的第193行(异常)

c.konsumatoriPunetField.textProperty().setValue("ertani");

最佳答案

加载FXML文件时,控制器由FXMLLoader创建(控制器类由FXML文件指定,因此这是唯一可以创建的类)。因此,如果在调用loader.getController()之前先调用load(),则返回的值将为null。因此,在您的代码中,c为空,并且您会得到一个空指针异常。

请注意,实际上在这里调用loader.load()并没有帮助。它可以修复空指针异常,但是您当然可以加载FXML文件定义的UI的新实例和控制器的新实例。因此,您要为其设置文本的文本字段将不是所显示的文本字段,并且什么也不会发生。

由于您在创建的窗口上使用showAndWait(),因此设置文本的最简单方法是在MainWindowController调用完成后将其重新放入showAndWait()showAndWait()阻止执行直到窗口关闭,因此文本字段在窗口关闭之前不会改变。

首先在ZgjedhKonsumatorinController中定义一个用于检索文本的方法:

public class ZgjedhKonsumatorinController {

    @FXML
    public void zgjedhKonsumatorin()
    {
        main.zgjedhkonsumatorinstage.close();
    }

    public String getText() {
        // in real life you can get text from the controls in ZgjedhKonsumatorinFXML.fxml
        return "ertani" ;
    }
}


现在回到MainWindowController中,您可以执行以下操作:

public class MainWindowController {

    @FXML
    // Note: it is ALWAYS a mistake to initialize @FXML-injected fields.
    // Just declare them and let the FXMLLoader initialize them
    // (that is the whole point of @FXML)
    private TextField konsumatoriPunetField ;

    private ZgjedhKonsumatorinController zgjedhKonsumatorinController ;

    @FXML
    private void initialize()
    {
        FXMLLoader loader5 = new FXMLLoader();
        loader5.setLocation(getClass().getResource("ZgjedhKonsumatorinFXML.fxml"));
        BorderPane border5 = new BorderPane();
        border5 = loader5.load();
        zgjedhKonsumatorinController = loader.getController();
        Scene scene5 = new Scene(border5);
        zgjedhkonsumatorinstage.setScene(scene5);
        zgjedhkonsumatorinstage.setTitle("Pit Stop");
        zgjedhkonsumatorinstage.initModality(Modality.WINDOW_MODAL);
        zgjedhkonsumatorinstage.initOwner(MainFXMLController.mainFXMLStage);
    }

    @FXML
    public void zgjedhKonsumatorin()
    {
        zgjedhkonsumatorinstage.showAndWait();
        konsumatoriPunetField.setText(zgjedhKonsumatorinController.getText());
    }

}

10-08 06:45