【问题标题】:JavaFX custom ChoiceBox not instantiated at startupJavaFX 自定义 ChoiceBox 在启动时未实例化
【发布时间】: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


    【解决方案1】:
    duree.valueProperty().get().dureeProperty().bindBidirectional(pel.dureeProperty());        
    
                                     |                                    |           
    

    ->空值可能来自|

    嗯,好像方法:

     public IntegerProperty dureeProperty() {
            return duree;
     }
    

    返回 null 导致默认构造函数未初始化 duree 属性。


    1)一种解决方案是将方法修改为:

    //lazy
    public IntegerProperty dureeProperty() {
           if(duree == null) //if duree has not been initialized
             duree = new SimpleIntegerProperty();
            return duree;
    }
    

    或2)您可以更改默认构造函数来初始化private IntegerProperty duree;属性,虽然解决方案1更好。

    【讨论】:

    • NULL 指针来自变量:private ChoiceBox duree; “duree” 为 NULL,而我像其他 ChoiceBox 一样放置 @FXML 注释
    • @François F. 我的意思是private IntegerProperty duree;。尝试用提供的代码替换代码,如果它不起作用,请在问题上添加错误堆栈跟踪:)
    • 我错了 duree ChoiceBox,问题出在 FXML 文件中。但要回答你,看我设置了一个bindDirectional。这样,我希望另一个 IntegerProperty 允许选择正确的 DureeChoiceBoxElement 项,与私有 IntegerProperty duree 进行比较;价值。我正在尝试这种方式。谢谢。
    • @François F. 如果没有堆栈跟踪,我无法猜测所有内容 :)。还有 pel 中的 duree.valueProperty().get().dureeProperty().bindBidirectional(pel.dureeProperty()); 是什么?虽然我建议在您的代码中提供lazy evaluation...以避免将来出现空指针。
    猜你喜欢
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2021-12-12
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多