我有一个用TitledPanes动态填充的Accordion窗格。每个TitlePane都需要保存一个子AnchorPane,它将显示该对象的相关信息。子项AnchorPane保存在单独的FXML文件中,需要在TitledPane扩展时加载。
这是我正在尝试使用的控制器:
public class PeopleViewController {
DataSource data;
@FXML
private Accordion accordion;
@FXML
private AnchorPane detailAnchor;
@FXML
private TableView<HashMap<String, String>> dataTable;
@FXML
private TableColumn<HashMap<String, String>,String> propertyiesColumn;
@FXML
private TableColumn<HashMap<String, String>,String> detailsColumn;
public ScheduleViewController(){
data = new DataSource();
}
@FXML
private void initialize() {
ObservableList<String> nameList = FXCollections.observableArrayList(dataSource.getNames());
for(String name:nameList){
accordion.getPanes().add(buildTitledPane(name));
}
accordion.setVisible(true);
}
private TitledPane buildTitledPane(final String text){
final TitledPane tp = new TitledPane();
tp.setText(text);
tp.expandedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(
ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue) {
if(tp.isExpanded()){
getPersonData(tp);
}
}
});
return tp;
}
@FXML
private void getPersonData(TitledPane tp){
try {
detailAnchor = FXMLLoader.load(getClass().getResource("PersonDetail.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
//Will add data to the table here
tp.setContent(detailAnchor);
}
但是getPersonData()会抛出java.lang.reflect.InvocationTargetException ...引起:java.lang.NullPointerException。我显然没有正确加载文件。我做错了什么?