我有一个无法加载我为其创建的“MediaFile”对象的TableView。无论我使用哪种(不同的单元工厂和单元值facotries)方法来显示MediaFile对象。当我调用playListTableView.setItems(DBUtils.getList());
时,我得到一个空指针异常。这个MediaFile对象列表不为空 - 我可以看到它打印到控制台。任何有关解决这个问题的建议都会非常感激。
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();
}
}