I have these two classes and want to use the method of MyData to insert data in MyBean.
@Named
@RequestScoped
public class MyBean implements Serializable {
@Inject
private MyData myData;
public MyBean() {
// NullPointerException, because myData is null
this.dataList = myData.method();
}
...
}
@Singleton
class MyData {
method(){
...
}
}
MyData on the other Hand injects a controller class myHandler:
@Singleton
public class myHandler {...}
I keep getting error messages, that myData.method()
is null and have no idea about it. Maybe I haven't fully understood what CDI injection means?
Edit: These are the fully qualified annotations.
import javax.ejb.Singleton;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
Edit2: The container used is a wildfly 11. Since I use Maven too, there is not much I could do wrong with my project structure because I have to follow the Maven rules.
As I mentioned before, the server works fine for crud operations on my database, where I used CDI and JSF before. I just added MyBean and MyData to my project.
I tried @Startup
, results have not changed.
Edit3: I don't think it's a duplicate because I tried to use @DependsOn()
annotation and still get null back. The NullException doesn't come from the bean but from JavaScript, where I want to insert a String with JSF later being processed to a chart. The stacktrace says, that this function passes null value to the member variable in the contructor of the bean.