0
votes

im trying to add some items to my View Table . im creating a ObservableList and pass it to the setItem method but it throws null Exception .

Here is the latest attempt :

        javafx.scene.control.TableView table = controller.friendsList;

        /*
        the controller.friendList is defined as Below :
        @FXML
        public javafx.scene.control.TableView friendsList;
        */

        ObservableList friendData = FXCollections.observableArrayList();

        for(Client friend : friends )
        {

            friendData.add(friend.getUsername());

        }
        //The friendData is fine ( according to the debugging ) it includes 2 Strings
        table.setItems(friendData);
        table.setEditable(false);
        table.setVisible(false);
        table.setVisible(true);

    }

whats wrong with the code ?

EDIT:

now i get the controller from the FXMLLoader as the answere says . but it's still the same . all the field with the @FXML anotation are still null .

here is how i get the controller :

public userSceneController controller;
FXMLLoader fxmlLoader = new FXMLLoader();
Parent root = fxmlLoader.load(getClass().getResource("userScene.fxml"));
controller = (userSceneController) fxmlLoader.getController();
1
Which line throws the null pointer exception? - James_D
as i said in the title : table.setItems(friendData); - Mohammad Siavashi
Then table is null, which means that controller.friendsList is null. Show how you are getting the reference to the controller and how friendsList is initialized in the controller (which is presumably via FXML). (To really get help with this, you will probably need to create a minimal reproducible example.) - James_D
i've added the controller to the code above . and the friendsList is defined as it is shown in comment in the code . im using my textField and Buttons this way and i have no problem with them . am i missing something ? - Mohammad Siavashi
Well you create the controller instance and never ever initialize the friendsList field, which is @FXML-annotated, so presumably it is intended to be initialized as part of loading an FXML file. Since this controller instance isn't created as part of loading an FXML file, its friendsList field will not be initialized. You need to get the instance of the controller created when you load the FXML, not just create some arbitrary new instance. - James_D

1 Answers

1
votes

Since your TableView is a @FXML-annotated field in the controller, it is initialized in the controller instance created by the FXMLLoader. In other words, presumably at some point in the code you didn't actually show, you create an FXMLLoader and call load() on it. As part of that load process, the FXMLLoader creates an instance of the controller and initializes the @FXML-annotated fields with references to the controls displayed in the UI.

You are presumably intending to configure the table view that is displayed in the UI; in order to do that, you need to reference the controller that the FXMLLoader created for you.

However, what you actually do in your code is to create a new controller instance:

Controller controller = new Controller();

Obviously since this in not the controller created as part of the FXMLLoader.load() process, it never had its friendsList field initialized.

Instead, you need to retrieve the controller instance from your FXML loader. In order to do this, you must create an FXMLLoader with a location properly set, and then use the instance method FXMLLoader.load(), not the static method FXMLLoader.load(URL):

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("userScene.fxml"));
Parent root = (Parent) fxmlLoader.load();
controller = (userSceneController) fxmlLoader.getController();