在给出 fx:id 后,我的控制器中出现了一个空指针,错误仍然存在
I am getting a nullpointer inside my controllers after giving the fx:id, still the error persists
我有一个 fxml 表示,其中我的 fx:id 定义为 ContentPane
<center>
<Pane fx:id="ContentPane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
在主应用程序中,我有
public class Home_pageController extends Application {
@FXML
private Pane ContentPane;
/**
* Initializes the controller class.
*/
/**
* Initializes the controller class.
*/
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("home_page.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Some scene");
stage.setScene(scene);
stage.show();
MyGrid();
}
public static void main(String[] args) {
launch(args);
}
public void MyGrid() throws IOException {
GridPane root = new GridPane();
Label test = new Label("Test");
root.add(test,0,0);
ContentPane.getChildren().clear();
ContentPane.getChildren().add(root);
}
}
我收到以下错误
java.lang.NullPointerException
at controllers.Home_pageController.MyGrid(Home_pageController.java:70)
at controllers.Home_pageController.start(Home_pageController.java:62)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
如果在fxml中指定了fx:controller
属性,FXMLLoader
使用指定为属性值的class的构造函数来构造控制器的新实例。
控制器的现有实例不会自动重用。
这就是为什么启动的应用程序实例的ContentPane
字段从未被修改并保持null
的原因。
我有一个 fxml 表示,其中我的 fx:id 定义为 ContentPane
<center>
<Pane fx:id="ContentPane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
在主应用程序中,我有
public class Home_pageController extends Application {
@FXML
private Pane ContentPane;
/**
* Initializes the controller class.
*/
/**
* Initializes the controller class.
*/
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("home_page.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Some scene");
stage.setScene(scene);
stage.show();
MyGrid();
}
public static void main(String[] args) {
launch(args);
}
public void MyGrid() throws IOException {
GridPane root = new GridPane();
Label test = new Label("Test");
root.add(test,0,0);
ContentPane.getChildren().clear();
ContentPane.getChildren().add(root);
}
}
我收到以下错误
java.lang.NullPointerException
at controllers.Home_pageController.MyGrid(Home_pageController.java:70)
at controllers.Home_pageController.start(Home_pageController.java:62)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
如果在fxml中指定了fx:controller
属性,FXMLLoader
使用指定为属性值的class的构造函数来构造控制器的新实例。
控制器的现有实例不会自动重用。
这就是为什么启动的应用程序实例的ContentPane
字段从未被修改并保持null
的原因。