PDStat
PDStat

Reputation: 5855

Weld producer methods not injecting - NPE

I am trying to create a simple example of using classes that have producer methods, with the aim that the producer method injects the dependency instead of Weld trying to create the bean. Which is why I've @Vetoed the DefaultEngine I also wish to stick with the @Default @Any qualifiers

So I have the following

Engine.java

public interface Engine
{
    void start();
    void stop();
}

DefaultEngine.java

@Vetoed
public class DefaultEngine implements Engine
{

    public void start()
    {
        System.out.println("Cough cough vrummmmm");
    }

    public void stop()
    {
        System.out.println("Phhhut clank");
    }

}

Car.java

public class Car
{
    @Inject
    private Engine engine;

    public void startCar()
    {
        engine.start();
    }

    public void stopCar()
    {
        engine.stop();
    }

}

App.java

public class App 
{
    public static void main(@Observes ContainerInitialized event)
    {
        Car car = new Car();
        car.startCar();
        car.stopCar();
    }
}

EngineProducer.java

public class EngineProducer
{

    @Produces
    public Engine getEngine()
    {
        return new DefaultEngine();
    }

}

And I have the beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/beans_1_1.xsd">
</beans>

But it seems the Engine is not injected as I receive the following.

Exception in thread "main" java.lang.NullPointerException
    at com.test.testing.Car.startCar(Car.java:12)
    at com.test.testing.App.main(App.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:88)
    at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:78)
    at org.jboss.weld.injection.MethodInvocationStrategy$SimpleMethodInvocationStrategy.invoke(MethodInvocationStrategy.java:129)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:308)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:274)
    at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:264)
    at org.jboss.weld.event.ObserverNotifier.notifySyncObservers(ObserverNotifier.java:271)
    at org.jboss.weld.event.ObserverNotifier.notify(ObserverNotifier.java:260)
    at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:154)
    at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:136)
    at org.jboss.weld.manager.BeanManagerImpl.fireEvent(BeanManagerImpl.java:673)
    at org.jboss.weld.environment.se.WeldContainer.initialize(WeldContainer.java:142)
    at org.jboss.weld.environment.se.Weld.initialize(Weld.java:544)
    at org.jboss.weld.environment.se.StartMain.go(StartMain.java:44)
    at org.jboss.weld.environment.se.StartMain.main(StartMain.java:53)

Upvotes: 3

Views: 534

Answers (1)

PDStat
PDStat

Reputation: 5855

OK found the answer, it appears Weld cannot manage beans created with new also it cannot inject bean instances into observers of container events. So the main App.java has to change to

public class App 
{
    @Inject
    private Car car;

    public void main(@Observes ContainerInitialized event)
    {
        car.startCar();
        car.stopCar();
    }
}

Upvotes: 1

Related Questions