我尝试使用JavaFX来构建GUI。当我使用两列,roomId和applianceId初始化表时,出现错误。
@FXML
private void initialize() {
// Initialize the appliance table with the two columns.
roomIdColumn.setCellValueFactory(cellData -> cellData.getValue().roomIdProperty());
applianceIdColumn.setCellValueFactory(cellData -> cellData.getValue().applianceIdProperty());
}
以下是设备类
private IntegerProperty applianceId;
private IntegerProperty roomId;
private IntegerProperty state;
public Appliance(int applianceId, Integer roomId) {
this.applianceId = new SimpleIntegerProperty(applianceId);
this.roomId = new SimpleIntegerProperty(roomId);
this.state = new SimpleIntegerProperty(0);
System.out.println("Room " + roomId + " Appliance " + applianceId + " created");
}
public IntegerProperty roomIdProperty() {
return roomId;
}
public int getApplianceId() {
return applianceId.get();
}
public IntegerProperty applianceIdProperty() {
return applianceId;
}
public abstract int getPower();
public IntegerProperty getState(){
System.out.println("appliance " + applianceId + " state = " + state);
return this.state;
}
错误显示:
javafx.fxml.LoadException:
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2575)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
at view.MainApplication.showApplicationOverview(MainApplication.java:89)
at view.MainApplication.start(MainApplication.java:59)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$54/365777365.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/2058173423.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2570)
... 14 more
Caused by: java.lang.NullPointerException
at view.ApplicationOverviewControl.initialize(ApplicationOverviewControl.java:46)
... 24 more
为什么initialize方法错误运行?我只是按照网络上的教程并更改一些标识符。在roomIdProperty()和applianceIdProperty()中似乎有些不对劲。