0

So, in my initialize method, I am trying to bind certain buttons so that they are only enabled when an item is selected. But I keep getting a null pointer exception when I do that. I also get null pointer exception on the line in my start method

photos.setItems(FXCollections.observableArrayList(currAlbum.getPhotos()));

Granted at the start of this application, photos would be empty but shouldn't this just create an empty listview? Is there a problem with creating a listview of a photo object?

public class albumController {
@FXML AnchorPane root;
@FXML Label albumNameLabel, albumNameBackgrounLabel, photoLibraryLabel, captionInstruction, captionLabel, tagsInstruction, tagLabel, tagInstruction, valueInstruction, dateInstruction, dateLabel, instructionLabel;
@FXML ListView<Photo> photos;
@FXML Button displayButton, backButton, logOutButton, quitButton, renameButton, createTagButton,addTagButton, deleteTagButton, addPhotoButton, deletePhotoButton, copyMoveButton, moveButton;
@FXML TextField renameField, createTag, createTagValue, addTagValue;
@FXML ChoiceBox<String> addTag, deleteTag, deleteTagValue;
User user;
UserList userList;
Album currAlbum;
/**
 * Initializes the user, his albums, and his photos 
 * @param user Current user logged in
 * @param currAlbum Current album user is viewing
 */
public void start(Album currAlbum) {
    this.currAlbum=currAlbum;
    albumNameLabel.setText(currAlbum.getName());
    /*
    photos.setCellFactory(new Callback<ListView<Photo>, ListCell<Photo>>(){

                @Override
                public ListCell<Photo> call(ListView<Photo> p) {

                    return new PhotoCell();
                }
            }); 
            */
    photos.setItems(FXCollections.observableArrayList(currAlbum.getPhotos()));

    //photos.getSelectionModel().select(0);
    /*
     * if there is at least one photo, select it and fill in the caption and tag labels
     */
    //initialize tag presets
    addTag.setItems(FXCollections.observableArrayList(user.getTagNames()));
}

public void initialize() {

    copyMoveButton.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    moveButton.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    deletePhotoButton.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    renameButton.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    createTagButton.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    addTagButton.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    deleteTagButton.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    displayButton.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    createTag.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    createTagValue.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    addTag.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    addTagValue.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    deleteTag.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    deleteTagValue.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    renameField.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    renameField.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));
    renameField.disableProperty().bind(Bindings.isNull(photos.getSelectionModel().selectedItemProperty()));

    /*
    captionLabel.textProperty().bind(Bindings.createStringBinding(() -> {
        var selectedItem = photos.getSelectionModel().getSelectedItem() ;
        if (selectedItem == null) {
            captionLabel.setText("");
        } 
        else {
            captionLabel.setText(selectedItem.getCaption());
        }
    }), 
    photos.getSelectionModel().selectedItemProperty()
    );
    */
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • what do you mean by it does not have an FXML annotation? I don't think it was supposed to. Current album is passed in with the start method and initialized with the code I have. It's not a button or anything FXML related –  Apr 15 '20 at 17:42
  • It just seems like `photos` is null. – James_D Apr 15 '20 at 18:46
  • Can you create an observable list when photos is null? When the application runs for the first time, there shouldn't be any photos in it. So, how am I supposed to make an observable array list if no photos have been added yet? –  Apr 15 '20 at 20:50
  • `photos` is a `ListView`. It’s null. – James_D Apr 15 '20 at 20:51

0 Answers0