2

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.

flyby
  • 111
  • 1
  • 10
  • have you configured that bean somewhere? – Stultuske Jan 19 '18 at 14:05
  • My IDE created a beans.xml. – flyby Jan 19 '18 at 14:07
  • and, have you configured your bean in there? – Stultuske Jan 19 '18 at 14:08
  • I have set bean-discovery-mode to all, so this should be enough. Other beans in my project which use injection work just fine, too. – flyby Jan 19 '18 at 14:12
  • There are many questions to answer before the problem can be found: 1. What (kind of) container are you running this in? 2. What are the fully qualified names of the annotations you're using? 3. Does this work when you just use plain JSF managed beans? – ernest_k Jan 19 '18 at 14:25
  • What do you mean by container? I thought ManagedBean annotations is deprecated? – flyby Jan 20 '18 at 15:19
  • Can u try to put `@Startup` annotation below `Singleton` and see? – Ahmed Raaj Jan 22 '18 at 06:10
  • By container he meant whether you are running in Wildfly (Java EE container), TOmcat (servlet container) or just in SE. But my guess would be archives - what is the deployment structure? Can you make sure all parts of deployment are recognized as bean archives? (easiest way is to put beans.xml to all of them). Why I have this suspicion? Since you do not get deployment error like unsatisfied/ambiguous dependency, it looks like your `MyBean` is not even identified as bean, not picked up by CDI at all. – Siliarus Jan 22 '18 at 07:37

1 Answers1

-2

Have you used EJB Singleton by mistake? javax.ejb.Singleton is not recognised by CDI and so it will not be picked up.

For CDI, use either javax.inject.Singleton or @ApplicationScoped.

  • It doesn't work with changed annotation, too. – flyby Jan 20 '18 at 15:22
  • 1
    Also, CDI does recognize EJB Singleton as bean and it is very commonly used together. In fact, CDI's Singleton might behave differently from what you expect (see docs if intrigued). – Siliarus Jan 22 '18 at 07:33