【发布时间】:2016-11-23 13:54:35
【问题描述】:
我正在编写一个 JavaFX 应用程序。我用 SceneBuilder 8.2 创建了 GUI。 我已经放了一些选择框,但使用的是 Java 类型,而不是自定义类型。 而且效果很好。 但是使用自定义类型,选择框没有实例化,为什么? 代码下方:
public class PelController {
//instanciation is OK
@FXML
private ChoiceBox<PeriodeEnum> fCalculInteret;
//not instanciated at launch
@FXML
private ChoiceBox<DureeChoiceBoxElement> duree;
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
@FXML
private void initialize() {
//fCalculInteret is NOT NULL, automatically instantiated at startup.
fCalculInteret.valueProperty().bindBidirectional(pel.fCalculInteretProperty());
//duree is NULL causing NullPointerException
duree.valueProperty().get().dureeProperty().bindBidirectional(pel.dureeProperty());
}
}
自定义类型:
public class DureeChoiceBoxElement extends ObservableValueBase<DureeChoiceBoxElement> {
private IntegerProperty duree;
public DureeChoiceBoxElement() {
}
public DureeChoiceBoxElement(int duree) {
this.duree = new SimpleIntegerProperty(duree);
}
public IntegerProperty dureeProperty() {
return duree;
}
public Integer getDuree() {
return duree.getValue();
}
@Override
public DureeChoiceBoxElement getValue() {
// TODO Auto-generated method stub
return new DureeChoiceBoxElement(duree.get());
}
public void setDuree(Integer duree) {
this.duree.setValue(duree);
}
@Override
public String toString() {
return duree + " an";
}
}
你能解释一下为什么会出现这个 NullPointer。谢谢
【问题讨论】:
标签: javafx-2 fxml scenebuilder