我有以下类(class)
public class InitialSetupWizardData {
private final SimpleStringProperty licence_type = new SimpleStringProperty(this,"licenceType","");
public String getLicence_type() {
return licence_type.get();
}
public SimpleStringProperty licence_typeProperty() {
return licence_type;
}
public void setLicence_type(String licence_type) {
this.licence_type.set(licence_type);
}
}
现在我想将其注入(inject)到我的 javafx Controller 中。我添加了以下内容
public class Controller implements Initializable {
@Inject
InitialSetupWizardData data;
@Override
public void initialize(URL location, ResourceBundle resources) {
data.setLicence_type("Am cool");
}
}
上面总是在 data.set 上抛出空指针异常...我在使用 google Juice 库时错过了什么
最佳答案
注入(inject)不会自动发生。对于 FXMLLoader
创建的 Controller 对象,不会发生注入(inject)。
要更改此设置,请在加载 fxml 时使用 controllerFactory
。以下示例需要一个 Injector
,该注入(inject)器的设置方式可以正确创建 Controller 类的实例:
Injector injector = ...
FXMLLoader loader = new FXMLLoader(url);
loader.setControllerFactory(injector::getInstance);
Parent parent = loader.load();
关于java - 依赖注入(inject)在javafx谷歌果汁中抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54840552/