JavaFX - TableView 的问题(NullPointerException)
JavaFX - problem with TableView (NullPointerException)
我在使用 JavaFX 中的 TableView 控件时遇到了一个大问题。这是我第一次使用它,我不知道我做错了什么。
我的代码:
主要class:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器class:
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableView;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
public class Controller {
@FXML
private TableView<Person> peopleTableView;
@FXML
public void openNewWindow(){
try {
Parent root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
Stage mainWindow = new Stage();
mainWindow.setTitle("App");
mainWindow.setScene(new Scene(root, 1200, 600));
mainWindow.initModality(Modality.APPLICATION_MODAL);
mainWindow.show();
ObservableList<Person> people = FXCollections.observableArrayList();
people.add(new Person("Walter", "White"));
people.add(new Person("Gus", "Fring"));
peopleTableView.setItems(people);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
人class:
package sample;
import javafx.beans.property.SimpleStringProperty;
public class Person {
private SimpleStringProperty firstName = new SimpleStringProperty("");
private SimpleStringProperty lastName = new SimpleStringProperty("");
public Person(String firstName, String lastName) {
this.firstName.set(firstName);
this.lastName.set(lastName);
}
public String getFirstName() {
return firstName.get();
}
public SimpleStringProperty firstNameProperty() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public String getLastName() {
return lastName.get();
}
public SimpleStringProperty lastNameProperty() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
}
还有两个 FXML 代码,一个用于 window,当我们 运行 代码时显示:
sample.fxml:
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml">
<center>
<Button fx:id="openWindowButton" text="Open new window" onAction="#openNewWindow"/>
</center>
</BorderPane>
还有一个用于 TableView 的新阶段:
mainWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<BorderPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<TableView fx:id="peopleTableView">
<columns>
<TableColumn text="First Name">
<cellValueFactory>
<PropertyValueFactory property="firstName"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="Last Name">
<cellValueFactory>
<PropertyValueFactory property="lastName"/>
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</center>
</BorderPane>
所以问题是 - 我无法设置 TableView 的项目,因为我得到一个异常,它是空的,即使它清楚地显示在(我相信)它实际存在的新阶段。
错误代码:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access00(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
... 47 more
Caused by: java.lang.NullPointerException
at TableView.issue/sample.Controller.openNewWindow(Controller.java:35)
... 59 more
问题是您正尝试对两个 FXML 文件使用相同的控制器 class(我认为,假设这意味着您两次都将拥有相同的控制器实例,但事实并非如此).
在您加载 sample.fxml
时创建的 Controller
实例上调用 openNewWindow()
方法。该 FXML 文件没有名为 peopleTableView
的字段,因此 peopleTableView
在该实例中为空,并且 peopleTableView.setItems(...)
导致空指针异常。
peopleTableView
字段在加载 mainWindow.fxml
时创建的 Controller
实例中初始化,但没有对该实例调用任何方法。
解决方案是为每个 FXML 文件使用不同的控制器 class(您基本上应该始终这样做)。您可以在 mainWindow.fxml
的控制器中使用 initialize()
方法来初始化 table 数据。
这是 sample.fxml
的控制器:
public class LoginController {
@FXML
public void openNewWindow(){
try {
Parent root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
Stage mainWindow = new Stage();
mainWindow.setTitle("App");
mainWindow.setScene(new Scene(root, 1200, 600));
mainWindow.initModality(Modality.APPLICATION_MODAL);
mainWindow.show();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
这里是 mainWindow.fxml
的控制器:
public class MainController {
@FXML
private TableView<Person> peopleTableView ;
@FXML
private void initialize() {
ObservableList<Person> people = FXCollections.observableArrayList();
people.add(new Person("Walter", "White"));
people.add(new Person("Gus", "Fring"));
peopleTableView.setItems(people);
}
}
现在只需更新两个 FXML 文件中的 fx:controller
属性。
如果您需要将数据从一个控制器传递到下一个控制器,请参阅 Passing Parameters JavaFX FXML
例如,如果你想在第一个控制器的openNewWindow()
方法中加载table数据(这看起来不自然,但作为一个例子),你可以这样做:
public class LoginController {
@FXML
public void openNewWindow(){
try {
// Note using FXMLLoader instance, not static load(URL) method:
FXMLLoader loader = new FXMLLoader(getClass().getResource("mainWindow.fxml"));
Parent root = loader.load();
Stage mainWindow = new Stage();
mainWindow.setTitle("App");
mainWindow.setScene(new Scene(root, 1200, 600));
mainWindow.initModality(Modality.APPLICATION_MODAL);
mainWindow.show();
ObservableList<Person> people = FXCollections.observableArrayList();
people.add(new Person("Walter", "White"));
people.add(new Person("Gus", "Fring"));
MainController mainController = loader.getController();
mainController.initializeTableData(people);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
和
public class MainController {
@FXML
private TableView<Person> peopleTableView ;
public void initializeTableData(ObservableList<Person> people) {
peopleTableView.setItems(people);
}
}
但是,要再次完整讨论控制器之间的各种通信方式,请参阅 Passing Parameters JavaFX FXML
我在使用 JavaFX 中的 TableView 控件时遇到了一个大问题。这是我第一次使用它,我不知道我做错了什么。
我的代码:
主要class:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器class:
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableView;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
public class Controller {
@FXML
private TableView<Person> peopleTableView;
@FXML
public void openNewWindow(){
try {
Parent root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
Stage mainWindow = new Stage();
mainWindow.setTitle("App");
mainWindow.setScene(new Scene(root, 1200, 600));
mainWindow.initModality(Modality.APPLICATION_MODAL);
mainWindow.show();
ObservableList<Person> people = FXCollections.observableArrayList();
people.add(new Person("Walter", "White"));
people.add(new Person("Gus", "Fring"));
peopleTableView.setItems(people);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
人class:
package sample;
import javafx.beans.property.SimpleStringProperty;
public class Person {
private SimpleStringProperty firstName = new SimpleStringProperty("");
private SimpleStringProperty lastName = new SimpleStringProperty("");
public Person(String firstName, String lastName) {
this.firstName.set(firstName);
this.lastName.set(lastName);
}
public String getFirstName() {
return firstName.get();
}
public SimpleStringProperty firstNameProperty() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public String getLastName() {
return lastName.get();
}
public SimpleStringProperty lastNameProperty() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
}
还有两个 FXML 代码,一个用于 window,当我们 运行 代码时显示:
sample.fxml:
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml">
<center>
<Button fx:id="openWindowButton" text="Open new window" onAction="#openNewWindow"/>
</center>
</BorderPane>
还有一个用于 TableView 的新阶段:
mainWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<BorderPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<TableView fx:id="peopleTableView">
<columns>
<TableColumn text="First Name">
<cellValueFactory>
<PropertyValueFactory property="firstName"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="Last Name">
<cellValueFactory>
<PropertyValueFactory property="lastName"/>
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</center>
</BorderPane>
所以问题是 - 我无法设置 TableView 的项目,因为我得到一个异常,它是空的,即使它清楚地显示在(我相信)它实际存在的新阶段。
错误代码:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access00(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
... 47 more
Caused by: java.lang.NullPointerException
at TableView.issue/sample.Controller.openNewWindow(Controller.java:35)
... 59 more
问题是您正尝试对两个 FXML 文件使用相同的控制器 class(我认为,假设这意味着您两次都将拥有相同的控制器实例,但事实并非如此).
在您加载 sample.fxml
时创建的 Controller
实例上调用 openNewWindow()
方法。该 FXML 文件没有名为 peopleTableView
的字段,因此 peopleTableView
在该实例中为空,并且 peopleTableView.setItems(...)
导致空指针异常。
peopleTableView
字段在加载 mainWindow.fxml
时创建的 Controller
实例中初始化,但没有对该实例调用任何方法。
解决方案是为每个 FXML 文件使用不同的控制器 class(您基本上应该始终这样做)。您可以在 mainWindow.fxml
的控制器中使用 initialize()
方法来初始化 table 数据。
这是 sample.fxml
的控制器:
public class LoginController {
@FXML
public void openNewWindow(){
try {
Parent root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
Stage mainWindow = new Stage();
mainWindow.setTitle("App");
mainWindow.setScene(new Scene(root, 1200, 600));
mainWindow.initModality(Modality.APPLICATION_MODAL);
mainWindow.show();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
这里是 mainWindow.fxml
的控制器:
public class MainController {
@FXML
private TableView<Person> peopleTableView ;
@FXML
private void initialize() {
ObservableList<Person> people = FXCollections.observableArrayList();
people.add(new Person("Walter", "White"));
people.add(new Person("Gus", "Fring"));
peopleTableView.setItems(people);
}
}
现在只需更新两个 FXML 文件中的 fx:controller
属性。
如果您需要将数据从一个控制器传递到下一个控制器,请参阅 Passing Parameters JavaFX FXML
例如,如果你想在第一个控制器的openNewWindow()
方法中加载table数据(这看起来不自然,但作为一个例子),你可以这样做:
public class LoginController {
@FXML
public void openNewWindow(){
try {
// Note using FXMLLoader instance, not static load(URL) method:
FXMLLoader loader = new FXMLLoader(getClass().getResource("mainWindow.fxml"));
Parent root = loader.load();
Stage mainWindow = new Stage();
mainWindow.setTitle("App");
mainWindow.setScene(new Scene(root, 1200, 600));
mainWindow.initModality(Modality.APPLICATION_MODAL);
mainWindow.show();
ObservableList<Person> people = FXCollections.observableArrayList();
people.add(new Person("Walter", "White"));
people.add(new Person("Gus", "Fring"));
MainController mainController = loader.getController();
mainController.initializeTableData(people);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
和
public class MainController {
@FXML
private TableView<Person> peopleTableView ;
public void initializeTableData(ObservableList<Person> people) {
peopleTableView.setItems(people);
}
}
但是,要再次完整讨论控制器之间的各种通信方式,请参阅 Passing Parameters JavaFX FXML