Marcus Lanvers
Marcus Lanvers

Reputation: 411

Nullpointer Exception when trying to access Injected Bean

I have a maven multi module project. When trying to Access a injected bean i get a Nullpointer Exception.

this is the main Function that startsthe application

public class App {

    public static void main(String[] args) {
        System.out.println("Startpoint");
        DecisionMaker decisionMaker = new DecisionMaker();
        decisionMaker.run();
    }
}

this is what i do in the DecisionMaker

public class DecisionMaker {

@Inject
GameListener gm;

@Inject
BasicProductionManager basicProductionManager;

public DecisionMaker() {
    System.out.println("this is the decisionmaker");
    System.out.println(gm.toString());
}

so this is not an bean but a normal pojo

the gamelistener is a Applicationscoped bean which i want to inject.

@Named
@ApplicationScoped

public class GameListener extends DefaultBWListener {

@Inject
Event<OnFrameEvent> onFrameEvent;

public Mirror mirror = new Mirror();

public Game game;

public Player self;

@PostConstruct
public void init() {
    System.out.println("init listener");
}

the nullpointer gets thrown in the constructor of the DecisionMaker. The @PostConscrutct init method is not called

I looked into similar question but all i found is that i needthe PostConstruct method which i already have.

Upvotes: 0

Views: 2115

Answers (1)

Ramanlfc
Ramanlfc

Reputation: 8354

you can't instantiate DecisionMaker yourself. the whole point of CDI is to give it control of creating and managing beans.

check out this article to see how to use CDI in JAVA SE.

Upvotes: 1

Related Questions