tags:

views:

29

answers:

1

I've tried putting the remote interface of another Singleton bean into another. However, the remote object will always be null. Is there any other way I could get around it?

@Singleton
public class SingletonBean1 implements SingletonBean1Remote {

    @EJB
    SingletonBean2Remote singletonBean2Remote;

    ...

    public SingletonBean1() {
        ...

        singletonBean2Remote.anyMethod(); // returns NullPointerException

        ...
    }

}
A: 

The fact that it's a Singleton is immaterial. You have to initialize that reference to point to something besides null. As written, that's exactly what should happen.

The method that creates singleton #1 should get a reference to singleton #2.

duffymo
In my other EJB session bean implementation, the @EJB annotation takes care of the referencing without having to point it to something. Are you suggesting I do a singletonBean1Remote = new SingletonBean1Remote()?
Chris
You're right, dependency injection ought to be sorting this out. The rub is Singleton - there needs to be a factory class or method to tell the DI engine how to create the singletons. Perhaps that is missing. I'm a Spring user, so I can't advise on EJB3 as well as I'd like.
duffymo
Yeah, thanks anyway! (:
Chris
You can certainly solve it by bypassing the DI engine and initializing both yourself. That will get you past the NPE problem. Once you have that, you can worry about using the EJB3 idiom.
duffymo