0

In my Android application I have to initialize a lot of static Objects before the first Activity starts. From what I know, static variables are initialized when classes are loaded. So, with time the amount of static objects in project began to grow and now I'm getting NullPointerExceptions. In my case static objects may call other static objects in their constructors. So my question is: could some static variables be initialized before variables they depend on and thus cause NullPointersExceptions? Is that possible? Code example :

private static class HardwareManagersHolder implements HardwareManagers, IManagers {

        private final AtomicBoolean senderAcquire = new AtomicBoolean(false);
        private final AtomicInteger receiverAcquire = new AtomicInteger(0);
        public IAudioManager audioManager;
        public IVideoManager videoManager;
        public IVibrationManager vibrationManager;
        public IBatteryHelper batteryHelper;

        @Override
        public void configureManager() {
            audioManager = AudioHelper.getInstance();
            vibrationManager = VibrationManager.getInstance();
            videoManager =  VideoManager.getInstance();
            batteryHelper = BatteryHelper.getInstance();
        }

And an Object Holder:

public class VideoManager implements IVideoManager {
    private static class VideoManagerHolder {
            public static final VideoManager VIDEO_MANAGER_INSTANCE = new VideoManager();
        }


    public static VideoManager getInstance() {
        return VideoManagerHolder.VIDEO_MANAGER_INSTANCE;
    }

}

2
  • 2
    Can you provide some code or a github repository? Commented Oct 27, 2018 at 14:53
  • @thomas Added example. In this case VideoManager is null. And HardwareManagersHolder's configureManager() method is called in non-static constructor of static variable. Commented Oct 27, 2018 at 14:59

2 Answers 2

1

I tried to reconstruct your exception with the snippets you provided. I used the following code:

public interface IVideoManager {}


public class VideoManager implements IVideoManager {


  private static class HardwareManagersHolder {

    public IVideoManager videoManager;

    public void configureManager() {
      videoManager = VideoManager.getInstance();
    }
  }

  private static class VideoManagerHolder {

    public static final VideoManager VIDEO_MANAGER_INSTANCE = new VideoManager();
  }

  public static VideoManager getInstance() {
    return VideoManagerHolder.VIDEO_MANAGER_INSTANCE;
  }

  public static void main(String[] arg) {
    System.out.println("Start test");
    HardwareManagersHolder h = new HardwareManagersHolder();
    h.configureManager();
    if (h.videoManager == null) {
      System.out.println("VideoManager is null");
    }
    System.out.println("Test finished");
  }

}

This code works on my machine. If this code is not working on yours, there is some other fault.

Sign up to request clarification or add additional context in comments.

Comments

0

Are you initializing them in a static constructor? They would get called first for precisely this reason.

static 
{ 
    VIDEO_MANAGER_INSTANCE = new VideoManager();
}

5 Comments

No. Look at example.
Your complaint is that the call to getInstance() is getting kicked off before the singleton class instance is initialized, right?
I'm not sure. But that's the only assumption I have for now.
Then I have no idea why you're downvoting this. You can initialize static members of a class without an instance with a static constructor. Almost, by definition, the static constructors would have to get called before anything else does. If nothing else, it would rule out this as the problem.
My apologies. Stupid stackoverflow :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.