@Inject in Vaadin always returns NullPointerException

Hello Vaadin Users,

I understand that know that @Inject is not part of the CDI specs, but it seems not to work in my POJO in Vaadin. NullPointerExeeption is always thrown at the line where the event is fired.


dataConstraintEvent.fire(new DataConstraintEvent(DataConstraintEvent.FETCH, dataConstraintData));
(Please see code below)

The NPE probabaly means that dataConstraintEvent is not injected.

Additional Info:

  1. An empty “bean.xml” exist under my WEB-INF/
  2. Eclipse (Helios)
  3. Vaadin-6.5.1
  4. JBoss6 and Weld(CDI RI)

Please help, and thanks

Here is a sample code:
[i]

@javax.annotation.ManagedBean(value=“userModel”)
@javax.enterprise.context.SessionScoped
public class UserModel extends BasicWebModel implements Serializable{
private static final long serialVersionUID = 5556910561480414365L;

private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(UserModel.class);

public static final String RESOURCE_BUNDLE =” web.resources.locale.UserResource_en";

@Inject @Any
protected Event dataConstraintEvent;

protected UserModel(){
this(null);
}

protected UserModel(UserDetails userDetails){
super(new LocalizedStrings(RESOURCE_BUNDLE), userDetails);

}

/**

  • The objective of this method is to first select from the database all data

  • fields that belong to a specific entity and the extract to display only those

  • fields that a given user role is allowed access e.g., read or write
    */
    @PostConstruct
    protected void initDataConstraint() {

    try{
    DataConstraintData dataConstraintData = new DataConstraintData();
    dataConstraintData.setParentTable(UserData.TABLE_NAME);
    dataConstraintData.setDataLevel(DataLevel.FIELD.toString());

    dataConstraintEvent.fire(new DataConstraintEvent(DataConstraintEvent.FETCH, dataConstraintData));

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

}

[/i]

I’m using CDI in combination with Vaadin. I think the thing you might be missing is that the Servlet used by Vaadin should load the Vaddin application class with the help of CDI for it to be correctly intialized. I think you will get it running by following these steps

  1. Assert that the first lines in the web.xml contains the correct version web-app version
<?xml version="1.0" encoding="UTF-8"?>
  1. Remove the Vaadin and from the web.xml. I still keep my web.xml with the for “productionMode” but thats all I have in it at the moment.

  2. create a new annotated servlet that is injected with the CDI anotated Application class, mine looks something like this

@WebServlet(urlPatterns=“/*”, initParams={@WebInitParam(name=“widgetset”, value=“se.rocketscience.test.cdi.widgetset.CdiTestWidgetset”)})
public class TestServlet extends AbstractApplicationServlet {

@Inject
Instance<TestCdiVaadinApplication> applicationInstance;

@Override
protected Application getNewApplication(HttpServletRequest request)
		throws ServletException {
	return this.applicationInstance.get();
}

@Override
protected Class<? extends Application> getApplicationClass()
		throws ClassNotFoundException {
	return TestCdiVaadinApplication.class;
}

}

  1. The @WebInitParam with name=“widgetset” in the Servlet is pointing to the “*.gwt.xml” file (in my case “CdiTestWidgetset.gwt.xml”) that should be available in your project. It is very important that this is specified if you’re trying to use add-ons since add-on widgetsets are inherited in the gwt.xml file.

  2. My Application class looks something like this and is using EJB:s but you could use any other CDI stuff

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import com.vaadin.Application;

@SessionScoped
public class TestCdiVaadinApplication extends Application {

@EJB
TestServiceRemote testService;

@Override
public void init() {
	
}

}