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();
tableis null, which means thatcontroller.friendsListis null. Show how you are getting the reference to the controller and howfriendsListis 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_DfriendsListfield, 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, itsfriendsListfield 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