TAGS :Viewed: 4 - Published at: a few seconds ago

[ Application context always returned null ]

I have this class in my project:

public class MyClass extends Application {

    // some stuff

    private Context context;

    public MyClass() {
        processContext();
        APP_DIRECTORY = context.getFilesDir();
        PRIVATE_FILE = new File(APP_DIRECTORY, MD5(getIEMI()));
    }


    public void processContext() {
        do {
            this.context = this.getApplicationContext();
        } while (this.context==null);
    }

    // some other stuff, such as reading or writing above declared files

    public String getIEMI() {
        String IMEI;
        Boolean hasTelephony = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
        if(hasTelephony) {
            TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            IMEI = telephonyManager.getDeviceId();
        } else {
            IMEI = "IMEI_not_AVAIABLE";
        }
        return IMEI;
    }
}

But I get a NullPointerException (LogCat below) everywhere I call context, even if I tried to follow hints proposed here (a bug in the emulator). I can't use context = MyClass.this, because I always (and only) get a NullPointerException.

04-14 20:25:28.063: E/AndroidRuntime(1084): Caused by: java.lang.NullPointerException 04-14 20:25:28.063: E/AndroidRuntime(1084): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100) 04-14 20:25:28.063: E/AndroidRuntime(1084): at com.example.mypackage.MyClass.processContext(MyClass.java:71) // this is the line of APP_DIRECTORY = context.getFilesDir();

In fact, I'm not sure of having fully understood the definition of Context, in Android. I was quite confident that extending the class to the super Application class was sufficient.

I can't either construct the class with a parameter as the context, arriving from the caller (in such a way: (new MyClass(MainActivity.this))) because I have the need to read and write files even from classes which are not extensions of Activity class.

I'm getting mad, to tell the truth...

-- EDIT --

For a test purpose, I re-wrote the class, so that it is constructed in this way:

    public MyClass(Context app_context) {
        APP_CONTEXT = app_context;
        APP_DIRECTORY = APP_CONTEXT.getFilesDir();
    }

It always returns a NullPointerException when calling getFilesDir().

-- EDIT 2 --

As suggested in the comments, I added a declaration of onCreate():

    @Override
    public void onCreate() {
        super.onCreate();
        Context context = this.getApplicationContext();
        APP_DIRECTORY = getFilesDir();
        LOGIN_FILE = new File(APP_DIRECTORY, name);

        Log.e("MyApp", "Main point reached.");
    }

And in this way everything looks good. But files declared in the onCreate() method seem always to be null, as if they are not declared at all: when I try to access to them, for reading or writing, I got a null pointer exception. And in fact "Main point reached." is never displayed, as if onCreate() is never called, nor processed, at all.

Answer 1


Application is Context. Try not to use constructor, but onCreate instead.

You may call getFilesDir directly without having private Context context;.

Answer 2


Replace this.context with getActivity()