0

I have a TableView that is failing to load the "MediaFile" objects I have created for it. No matter which (different cell factories and cell value facotries) methods I use to display the MediaFile objects. I get a null pointer exception when I call playListTableView.setItems(DBUtils.getList()); This list of MediaFile objects is not empty - I can see it print out to the console. Any suggestions on solving this would be much appreciated.

public class AudioPlayerFXMLController implements Initializable {

@FXML
private ListView playList;
@FXML
public static TableView<MediaFile> playListTableView;

@FXML
private TableColumn<MediaFile, String> pathColumn, artistColumn, titleColumn, albumColumn,
        trackNumberColumn, lengthColumn, descriptionColumn,
        ratingColumn, dateColumn, settingColumn, URLColumn, languageColumn,
        nowPlayingColumn, publishedByColumn, encodedByColumn, artworkURLColumn,
        trackIDColumn;

private static final String NEW_PLAYLIST = "New Playlist";
private static final String FRIEND_PLAYLIST = "Friend's Playlist";
private static final String LIBRARY = "Library";
private static ButtonCell bc;
ObservableList<String> observablePlayList;

@Override
public void initialize(URL url, ResourceBundle rb) {

    observablePlayList = FXCollections.observableArrayList();
    observablePlayList.add(NEW_PLAYLIST);
    observablePlayList.add(FRIEND_PLAYLIST);
    observablePlayList.add(LIBRARY);
    playList.setItems(observablePlayList);
    playList.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
        @Override
        public ListCell<String> call(ListView<String> list) {
            bc = new ButtonCell();
            return bc;
        }
    });

    playList.setEditable(true);
    playList.getSelectionModel().select(2);

    playList.setOnEditCommit(new EventHandler<ListView.EditEvent<String>>() {
        @Override
        public void handle(ListView.EditEvent<String> event) {

            System.out.println(event.getEventType());
            System.out.println(event.getNewValue());
        }
    });

    pathColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("path"));
    artistColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("artist"));
    titleColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("title"));
    albumColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("album"));
    trackNumberColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("trackNumber"));
    lengthColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("length"));
    descriptionColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("description"));
    ratingColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("rating"));
    dateColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("setting"));
    settingColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("url"));
    URLColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("language"));
    languageColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("nowPlaying"));
    nowPlayingColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("publisher"));
    publishedByColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("encodedBy"));
    encodedByColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("artworkURL"));
    artworkURLColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("artworkURL"));
    trackIDColumn.setCellValueFactory(new PropertyValueFactory<MediaFile, String>("trackID"));

    Callback<TableColumn<MediaFile, String>, TableCell<MediaFile, String>> cellFactory
            = new Callback<TableColumn<MediaFile, String>, TableCell<MediaFile, String>>() {
                @Override
                public TableCell call(TableColumn p) {
                    return new EditingCell();
                }
            };

    pathColumn.setCellFactory(cellFactory);
    artistColumn.setCellFactory(cellFactory);
    titleColumn.setCellFactory(cellFactory);
    albumColumn.setCellFactory(cellFactory);
    trackNumberColumn.setCellFactory(cellFactory);
    lengthColumn.setCellFactory(cellFactory);
    descriptionColumn.setCellFactory(cellFactory);
    ratingColumn.setCellFactory(cellFactory);
    dateColumn.setCellFactory(cellFactory);
    settingColumn.setCellFactory(cellFactory);
    URLColumn.setCellFactory(cellFactory);
    languageColumn.setCellFactory(cellFactory);
    nowPlayingColumn.setCellFactory(cellFactory);
    publishedByColumn.setCellFactory(cellFactory);
    encodedByColumn.setCellFactory(cellFactory);
    artworkURLColumn.setCellFactory(cellFactory);
    trackIDColumn.setCellFactory(cellFactory);

    Task loadDB = new Task() {

        @Override
        protected Object call() throws Exception {
            DBUtils.initDB();
            DBUtils.retrieveDB();
            return null;
        }
    };

    new Thread(loadDB).start();

    boolean dbFinishedLoading = false;
    while (dbFinishedLoading==false) {

        if (loadDB.isDone()) {
            System.out.println("done loading db!!!");
            System.out.println(DBUtils.getList());
            dbFinishedLoading = true;
            playListTableView.setItems(DBUtils.getList());
        }
    }

}

class EditingCell extends TableCell<MediaFile, String> {

    private TextField textField;

    public EditingCell() {
    }

    @Override
    public void startEdit() {
        if (!isEmpty()) {
            super.startEdit();
            createTextField();
            setText(null);
            setGraphic(textField);
            textField.selectAll();
        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();

        setText((String) getItem());
        setGraphic(null);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setText(null);
                setGraphic(textField);
            } else {
                setText(getString());
                setGraphic(null);
            }
        }
    }

    private void createTextField() {
        textField = new TextField(getString());
        textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
        textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> arg0,
                    Boolean arg1, Boolean arg2) {
                if (!arg2) {
                    commitEdit(textField.getText());
                }
            }
        });
    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }
}

class ButtonCell extends ListCell<String> {

    private TextField fieldForEditingCell;

    @Override
    public void startEdit() {

        if (!isEditable() || !getListView().isEditable()) {
            return;
        }

        super.startEdit();

        if (isEditing()) {
            fieldForEditingCell = new TextField("Enter your edit here");
            fieldForEditingCell.setOnKeyPressed(new EventHandler<KeyEvent>() {

                public void handle(KeyEvent keyPress) {
                    if (keyPress.getCode() == KeyCode.ENTER) {
                        String nameOfnewPlayList = fieldForEditingCell.getText();
                        observablePlayList.add(nameOfnewPlayList);
                        playList.setItems(null);
                        playList.setItems(observablePlayList);
                        playList.getSelectionModel().select(nameOfnewPlayList);

                    } else if (keyPress.getCode() == KeyCode.ESCAPE) {
                        cancelEdit();
                    }
                }
            });

            setGraphic(fieldForEditingCell);
            fieldForEditingCell.requestFocus();
            fieldForEditingCell.setEditable(true);

            fieldForEditingCell.focusedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    if (!newValue && fieldForEditingCell != null) {
                        commitEdit(fieldForEditingCell.getText());
                    }
                }
            });

            fieldForEditingCell.selectAll();

        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
        ImageView addSymbol;
        addSymbol = ImageViewBuilder.create().image(new Image("/images/ic_add_grey600_15dp.png")).build();
        addSymbol.fitHeightProperty();
        setText(getItem());
        setGraphic(addSymbol);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if ((this.getIndex() == 0) || (this.getIndex() == 1)) {
            ImageView addSymbol;
            addSymbol = ImageViewBuilder.create().image(new Image("/images/ic_add_grey600_15dp.png")).build();
            addSymbol.fitHeightProperty();
            setText(item);
            setGraphic(addSymbol);

            getStyleClass().add("custom_list-cell");
            //setStyle("-fx-background-color: #F5F5F5");
            //setStyle("-fx-hover-color: yellow");
        } else {
            setText(item);
            setGraphic(null);
        }
    }

}
//begin TableViewCell

public void tableDraggedOVer(DragEvent event) {
    Dragboard db = event.getDragboard();
    if (db.hasFiles()) {
        event.acceptTransferModes(TransferMode.COPY);
    }
    event.consume();

}

public void dropCompleted(DragEvent event) {
    Dragboard db = event.getDragboard();
    boolean success = false;

    if (db.hasFiles()) {
        event.acceptTransferModes(TransferMode.COPY);
        DBUtils.addToPLaylistTableFromDragAndDrop(db.getFiles());
        success = true;
    }

    event.setDropCompleted(success);
    event.consume();
}

}
user465001
  • 806
  • 13
  • 29
  • 1
    In addition to the static @FXML member (which is causing your null pointer), you also should not be busy waiting the db result in a while loop. In addition to the answer to the duplicate question, also see the answer to [JavaFX - Background Thread for SQL Query](http://stackoverflow.com/questions/14878788/javafx-background-thread-for-sql-query) to understand how to return the result of the DB lookup for the Task in an event driven manner so that you don't block the JavaFX application thread, thereby potentially freezing your application UI. – jewelsea Nov 05 '14 at 00:52
  • Thanks, jewelsea! It was the static member as you said. Thank you for the links as well. – user465001 Nov 05 '14 at 01:14

0 Answers0