依赖注入会在javafx google juice中抛出空指针异常

问题描述 投票:0回答:1

我有以下课程

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);
  }
}

现在我想将它注入我的javafx控制器。我添加了以下内容

public class Controller implements Initializable {
   @Inject
   InitialSetupWizardData data;

   @Override
   public void initialize(URL location, ResourceBundle resources) {
     data.setLicence_type("Am cool");
   }
}

以上总是在data.set抛出一个空指针异常...我错过了什么,因为我正在使用谷歌汁库

java javafx guice
1个回答
3
投票

注射不会自动发生。对于FXMLLoader创建的控制器对象,注入不会发生。

要更改此项,请在加载fxml时使用controllerFactory。以下示例需要以正确创建控制器类实例的方式设置Injector

Injector injector = ...
FXMLLoader loader = new FXMLLoader(url);
loader.setControllerFactory(injector::getInstance);
Parent parent = loader.load();
© www.soinside.com 2019 - 2024. All rights reserved.