Why getApplicationContext () in the activity constructor throws a null pointer exception?

advertisements

After some time spent bug hunting it turns out that :

public class MainActivity extends BaseActivity { // BaseActivity extends Activity

    public MainActivity() {
        super();
        getApplicationContext(); // NPE here
    }
}

Why ? Where is this documented ?
Froyo


Wait for the end of onCreate to call this method.

public class MainActivity extends BaseActivity { 

    public onCreate(Bundle savedInstanceState) {
        super(savedInstanceState);
        getApplicationContext(); //activity has a context now
    }
}