简体   繁体   English

尝试访问注入的Bean时出现Nullpointer异常

[英]Nullpointer Exception when trying to access Injected Bean

I have a maven multi module project. 我有一个Maven多模块项目。 When trying to Access a injected bean i get a Nullpointer Exception. 当尝试访问注入的bean时,我得到了Nullpointer异常。

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 这就是我在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 所以这不是豆子,而是普通的pojo

the gamelistener is a Applicationscoped bean which i want to inject. 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");
}

the nullpointer gets thrown in the constructor of the DecisionMaker. 空指针将被抛出到DecisionMaker的构造函数中。 The @PostConscrutct init method is not called @PostConscrutct初始化方法未调用

I looked into similar question but all i found is that i needthe PostConstruct method which i already have. 我调查了类似的问题,但发现的全部是我需要已经拥有的PostConstruct方法。

you can't instantiate DecisionMaker yourself. 您无法自己实例化DecisionMaker the whole point of CDI is to give it control of creating and managing beans. CDI的全部目的是使它具有创建和管理Bean的控制权。

check out this article to see how to use CDI in JAVA SE. 查看本文 ,了解如何在JAVA SE中使用CDI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 刷新异步任务后尝试访问适配器时出现nullPointer异常 - nullPointer exception when trying to access adapter after asynctask refresh junit:使用 mockmvc 测试控制器时注入的 bean 上的 NullPointer - junit: NullPointer on injected bean when testing controller with mockmvc 注入实例上的Guice nullpointer异常 - Guice nullpointer exception on injected instance 尝试初始化ChoiceBox时出现NullPointer异常 - NullPointer Exception when trying to Initialize ChoiceBox 尝试更新Sprite时出现NullPointer异常 - NullPointer Exception When Trying to Update Sprite 尝试修改整数时,它会引发NullPointer异常 - Integer throws NullPointer exception when trying to modify it 尝试接收UDP多播时出现Nullpointer异常 - Nullpointer exception when trying to receive UDP multicast 尝试使用SpringRunner进行单元测试时出现NullPointer异常 - NullPointer Exception when trying to Unit Test using SpringRunner 尝试访问Java ArrayList时的NullPointer <User> 那不是null - NullPointer when trying to access a Java ArrayList<User> that is NOT null 尝试注入具有@Required批注的bean,但获取nullpointer - Trying to inject a bean with @Required annotation but getting nullpointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM