尽管变量已设置,但为null

时间:2018-09-08 13:03:48

标签: java object events null listener

我构建了一个包含如下对象的对象:

public class TestThis implements Listener{

    private EventsManager eventsManager;
    private ConfigManager configManager;

    public TestThis() {
        eventsManager = new EventsManager();
        eventsManager.addListener(this);
        configManager = new ConfigManager(this);
    }

    public ConfigManager getConfigManager() {
        return configManager;
    }

    @Override
    public void configLoadedSuccessfully(Event event) {
        System.out.println(this.configManager); //OUTPUT NULL
        System.out.println(this.getConfigManager());//OUTPUT NULL
    }
}

configManager对象存在问题。 在构造中,一切正常,但configLoadedSuccessfully是事件,configManager为null,其evrywhere为null。

侦听器代码:

public interface Listener{
    public void configLoadedSuccessfully(Event event);
}

1 个答案:

答案 0 :(得分:1)

我添加了一个函数,该函数开始加载配置并更改var的创建顺序。

    public class TestThis implements Listener{

    private EventsManager eventsManager;
    private ConfigManager configManager;

    public TestThis() {
        eventsManager = new EventsManager();
        configManager = new ConfigManager(this);
        eventsManager.addListener(this);
        configManager.load();
    }

    public ConfigManager getConfigManager() {
        return configManager;
    }

    @Override
    public void configLoadedSuccessfully(Event event) {
        System.out.println(this.configManager);
        System.out.println(this.getConfigManager());
    }
}