我试图在文本字段中添加一些文本,但是当我点击按钮它显示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
P.S。这是ZgjedhKonsumatorinController中的第193行(例外)
c.konsumatoriPunetField.textProperty().setValue("ertani");
答案 0 :(得分:1)
当您加载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());
}
}