我的JavaFX应用程序线程有问题。
代码与员工搜索字段相关:
public class ShowEmployees {
@FXML
private TableView<EmployeeTO> tableView;
@FXML
private TextField filterField;
private ObservableList<EmployeeTO> observableList = FXCollections.observableArrayList();
private ObservableList<EmployeeTO> filteredData = FXCollections.observableArrayList();
public ShowEmployees(){
try {
PersonDA<EmployeeTO> personDA = new PersonDA();
observableList.addAll(personDA.select());
filteredData.addAll(observableList);
} catch (Exception e) {
e.printStackTrace();
}
observableList.addListener(new ListChangeListener<EmployeeTO>() {
@Override
public void onChanged(ListChangeListener.Change<? extends EmployeeTO> change) {
updateFilteredData();
}
});
}
@FXML
private void initialize() {
/////////////////
TableColumn personnelCode = new TableColumn("personal code");
personnelCode.setMinWidth(100);
personnelCode.setCellValueFactory(new PropertyValueFactory<EmployeeTO ,Integer >("personnelCode"));
/////////////////
TableColumn name = new TableColumn("name");
name.setMinWidth(100);
name.setCellValueFactory(new PropertyValueFactory<EmployeeTO ,String >("name"));
/////////////////
TableColumn lastname = new TableColumn("lastName");
lastname.setMinWidth(100);
lastname.setCellValueFactory(new PropertyValueFactory<EmployeeTO ,String >("lastName"));
tableView.getColumns().addAll(personnelCode,name,lastname);
tableView.setItems(filteredData);
// Listen for text changes in the filter text field
filterField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,String oldValue, String newValue) {
updateFilteredData();
}
});
}
private void updateFilteredData() {
filteredData.clear();
for (EmployeeTO p : observableList) {
Platform.runLater(() -> {
if (matchesFilter(p)) {
filteredData.add(p);
}
});
}
// Must re-sort table after items changed
reapplyTableSortOrder();
}
private boolean matchesFilter(EmployeeTO employee) {
String filterString = filterField.getText();
if (filterString == null || filterString.isEmpty()) {
// No filter --> Add all.
return true;
}
String lowerCaseFilterString = filterString.toLowerCase();
if (employee.getName().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
return true;
} else if (employee.getLastName().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
return true;
}
return false; // Does not match
}
private void reapplyTableSortOrder() {
ArrayList<TableColumn<EmployeeTO, ?>> sortOrder = new ArrayList<>(tableView.getSortOrder());
tableView.getSortOrder().clear();
tableView.getSortOrder().addAll(sortOrder);
}
}
搜索员工列表时发生以下错误。
错误:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at controller.ShowEmployees.matchesFilter(ShowEmployees.java:131)
at controller.ShowEmployees.lambda$updateFilteredData$0(ShowEmployees.java:106)
at controller.ShowEmployees$$Lambda$244/1338548772.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$163(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1037924535.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$164(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/786335548.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$140(WinApplication.java:100)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/705897616.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
有人可以帮我解决这个错误吗?
提前谢谢!