我编写了一个 JavaFX 用户界面 (fxml) 并将一个控制器类绑定到它,一切正常,直到我在我的应用程序中为 ListView 添加了一个 ObservableList 和一个 ChangeListener。当我运行应用程序时,ListView 看起来不错并显示正确的值,但是当我单击某些按钮时,会发生异常并且按钮将不起作用。这些按钮应该在单击时显示其他布局。异常表示未找到布局(例如 AddUser.fxml),但它存在并正常工作到现在!
控制器类的一些行:
public class FileSharingController implements Initializable {
private ObservableList<User> UsersListData=FXCollections.observableArrayList();
@FXML
private ListView<User> UsersList;
public FileSharingController(){
DatabaseManager DB=new DatabaseManager();
try {
UsersListData.addAll(DB.returnUsers());
} catch (SQLException e) {e.printStackTrace();}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
UsersList.setItems(UsersListData);
UsersListData.addListener(new ListChangeListener<User>(){
@Override
public void onChanged(Change<? extends User> c) {
System.out.println(c.getList());
}
});
}
@FXML
public void showAddUserLayout(){
try {Stage stage=new Stage();
AnchorPane rootPane=FXMLLoader.load(getClass().getResource("AddUser.fxml"));
Scene scene = new Scene(rootPane);
stage.setScene(scene);
stage.setTitle("Add User");
stage.show();
} catch (IOException e) {e.printStackTrace();}
}
}
用户类(模型):
package Model;
public class User {
private String Username;
private String Password;
public User(String Username,String Password){
this.Username=Username;
this.Password=Password;
}
public void Set(String Username,String Password){
this.Username=Username;
this.Password=Password;
}
public String toString(){
return Username+" "+Password;
}
}
当我单击“添加”按钮时,调用了 showAddUserLayout() 并出现以下异常:
java.lang.NullPointerException
/D:/workspace_FileSharing%20Serverside/FileSharing_ServerSide/bin/com/Shayan/FileSharing/Server/AddUser.fxml
at com.Shayan.FileSharing.Server.FileSharingController.updateUsersList(FileSharingController.java:144)
at com.Shayan.FileSharing.Server.FileSharingController.initialize(FileSharingController.java:50)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2152)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2694)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2683)
at com.Shayan.FileSharing.Server.FileSharingController.showAddUserLayout(FileSharingController.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1437)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3113)
at javafx.scene.Scene$ClickGenerator.access$8600(Scene.java:3051)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3333)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3164)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3119)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1559)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2261)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:228)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:722)
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
请帮忙!我不知道发生了什么...提前谢谢!