簡體   English   中英

嘗試訪問注入的Bean時出現Nullpointer異常

[英]Nullpointer Exception when trying to access Injected Bean

我有一個Maven多模塊項目。 當嘗試訪問注入的bean時,我得到了Nullpointer異常。

這是啟動應用程序的主要功能

public class App {

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

這就是我在DecisionMaker中所做的

public class DecisionMaker {

@Inject
GameListener gm;

@Inject
BasicProductionManager basicProductionManager;

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

所以這不是豆子,而是普通的pojo

gamelistener是我要注入的Applicationscoped bean。

@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");
}

空指針將被拋出到DecisionMaker的構造函數中。 @PostConscrutct初始化方法未調用

我調查了類似的問題,但發現的全部是我需要已經擁有的PostConstruct方法。

您無法自己實例化DecisionMaker CDI的全部目的是使它具有創建和管理Bean的控制權。

查看本文 ,了解如何在JAVA SE中使用CDI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM