문제

I wrote a JavaFX user interface (fxml) and bind a controller class to it, everything worked fine until I added an ObservableList and a ChangeListener for the ListView in my application. the ListView looks fine and shows the right values when I run the application, but when I click some buttons, Exceptions happen and buttons won't work. These are buttons that supposed to show the other layouts when clicked. the exception says that the layout (e.g. AddUser.fxml) not found, but it exists and worked correctly till now!

some lines of the controller class:

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();}
}
}

the User class (Model):

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;
}
}

when I click the "Add" button, the showAddUserLayout() called and I got the following Exceptions:

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)

Please help! I don't know What's happening... thanks in advance!

도움이 되었습니까?

해결책

Your posted code is incomplete. The FXMLLoader is loading the "AddUser.fxml" file. It also calling the initialize method of the controller. But the nullpointerexception appears at a line number 144 in updateUsersList() method. Check that line. And you may want to change the controller class of "AddUser.fxml" to other one than FileSharingController.

As per comment,
Does each Layout need a seperate controller?

Technically no, but semantically yes. If you set the same fx:controller in different FXML files then you will end up with multiple instances of that controller for every FXML file being loaded. If you use FXMLLoader.setController() among with FXMLLoader.setRoot() where the FXML files include fx:root instead of fx:controller then you will end up with only one instance of the controller which its initialize() method will be called separately for every FXML file being loaded. In both cases, however, each FXML file will semantically deal with only some part of the controller, i.e. init, inject, set, manipulate the parts it is interested in. IMO the FXML files should have no mutual domain, and if have then they must use normal java messaging mechanism through their own controllers. So having another AddUserController class will be more modular, maintainable and less headaches.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top