Dmitry
Dmitry

Reputation: 3038

Vaadin: CDI Inject is null after referencing

Good day.

There is an application (using a CDI add on):

@VaadinScoped(VaadinScope.APPLICATION)
public class AdminApplication extends AbstractCdiApplication {

   @Inject
   private Instance<Lang> lang;

   @Override
   public void init() {
    setMainWindow(new LoginWindow(this));
   }

   public void authenticate(String login, String password) throws Exception {
    lang.get(). ...
}
     ...

And the LoginWindow:

public class LoginWindow extends Window {
     ...
     public LoginWindow(AdminApplication application) {
       super("LoginWindow Login");
       this.application = application;
       initUI();
       initLoginListener();
     }
      private void initLoginListener() {
         btnLogin.addListener(new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
               try {
                   String username = (String) txtUsername.getValue();
                   String password = (String) txtPassword.getValue();

                   application.authenticate(username, password);
               } catch (Exception e) {
                showNotification(e.toString());
               }
           }
    });
}

The problem is when it is the application's initialization phase the lang.get() is not null, but when in the LoginWindow I call application.authenticate() method, the lang.get() is always null. It seem that when using a reference the Instance.get() method can't get the class' instance.

Does anybody know why this happens?

Upvotes: 0

Views: 1418

Answers (2)

Perty
Perty

Reputation: 921

I had this problem with Wildfly 8.1 and 8.2 but when I added an empty beans.xml in the WEB-INF it worked. So if the above doesn't work try that.

Upvotes: 1

Jan Galinski
Jan Galinski

Reputation: 12003

In your application, you create the LoginWindow via "new". That way, it is not managed by the CDI container. You have to inject the window to your application and then set it in the init() method.

Upvotes: 1

Related Questions