2

I am getting nullpointerexception in Application subclass. There I want a context that should be available to me all over the application and so I called getApplicationContext(), but this is causing an exception Below is logcat message:

12-22 17:15:35.639: E/AndroidRuntime(572): FATAL EXCEPTION: main
12-22 17:15:35.639: E/AndroidRuntime(572): java.lang.RuntimeException: Unable to instantiate application com.gaurav.contactmanager.model.ContactManagerApplication: java.lang.NullPointerException
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.app.LoadedApk.makeApplication(LoadedApk.java:466)
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3260)
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.app.ActivityThread.access$2200(ActivityThread.java:117)
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.os.Looper.loop(Looper.java:123)
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-22 17:15:35.639: E/AndroidRuntime(572):  at java.lang.reflect.Method.invokeNative(Native Method)
12-22 17:15:35.639: E/AndroidRuntime(572):  at java.lang.reflect.Method.invoke(Method.java:507)
12-22 17:15:35.639: E/AndroidRuntime(572):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-22 17:15:35.639: E/AndroidRuntime(572):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-22 17:15:35.639: E/AndroidRuntime(572):  at dalvik.system.NativeStart.main(Native Method)
12-22 17:15:35.639: E/AndroidRuntime(572): Caused by: java.lang.NullPointerException
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
12-22 17:15:35.639: E/AndroidRuntime(572):  at com.gaurav.contactmanager.model.ContactManagerApplication.<init>(ContactManagerApplication.java:10)
12-22 17:15:35.639: E/AndroidRuntime(572):  at java.lang.Class.newInstanceImpl(Native Method)
12-22 17:15:35.639: E/AndroidRuntime(572):  at java.lang.Class.newInstance(Class.java:1409)
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.app.Instrumentation.newApplication(Instrumentation.java:957)
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.app.Instrumentation.newApplication(Instrumentation.java:942)
12-22 17:15:35.639: E/AndroidRuntime(572):  at android.app.LoadedApk.makeApplication(LoadedApk.java:461)
12-22 17:15:35.639: E/AndroidRuntime(572):  ... 11 more

Below is the source-code:

package com.gaurav.contactmanager.model;

import android.app.Application;
import android.content.Context;
import android.util.Log;

public class ContactManagerApplication extends Application {
    public static Context context = null;
    public ContactManagerApplication() {
        Log.d("@gaurav", getApplicationContext()+"");
        context = getApplicationContext();
    }
}

and android-manifest declaration is as follows:

 <application
        android:name=".model.ContactManagerApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
  • have you tried after commenting `Log.d("@gaurav", getApplicationContext()+"");` line and removing static from `public static Context context = null;` ? – ρяσѕρєя K Dec 22 '12 at 13:18

5 Answers5

3

change it like this

public class ContactManagerApplication extends Application {
    public static Context context = null;

     @Override
    public void onCreate() {
        super.onCreate();
        Log.d("@gaurav", getApplicationContext()+"");
        context = getApplicationContext();

    }
confucius
  • 12,529
  • 10
  • 45
  • 66
1

Use ContactManagerApplication.this instead of getApplicationContext().

mjosh
  • 11,865
  • 7
  • 39
  • 72
0

You don't need to make constructor of Application class. It is automatically called by android framework, you have to override onCreate() method of Application class.

getApplicationContext() returns Context of the Application, which is assigned by the android framework, while you have put this in your own constructor, so android never called application pre-defined constructor, so the context object of application class is null at that time, which causing NullPointerException while ContextWrapper class is trying to access the Application's context.

Adil Soomro
  • 36,617
  • 9
  • 98
  • 146
  • so should i do the assignation part in onCreate() –  Dec 22 '12 at 13:20
  • if we want to initialize object with some default value then? – ρяσѕρєя K Dec 22 '12 at 13:21
  • use `onCreate()` of `Application` – Adil Soomro Dec 22 '12 at 13:22
  • @AdilSoomro :then how we send value to onCreate at the time of object creation? – ρяσѕρєя K Dec 22 '12 at 13:23
  • @ρяσѕρєяK what type of value?? you can assign values in `onCreate()`. `onCreate()` is kind of constructor in android framework. – Adil Soomro Dec 22 '12 at 13:28
  • @AdilSoomro : guptakvgaurav is creating constructor for `ContactManagerApplication` instead of `Application` so i don't think `Application` class prevent to creating an constructor of `ContactManagerApplication` class . so if you have any reference then plz provide it – ρяσѕρєя K Dec 22 '12 at 13:36
  • `ContactManagerApplication` is subclassing `Application` and OP has defined `ContactManagerApplication` as application name in manifest which means, this class will be called, when android framework will try to load the application. So whenever you want to share data across application, you can use this method of subclassing `Application` with your Application's exact name, Have a [look here](http://stackoverflow.com/a/708317/593709) – Adil Soomro Dec 22 '12 at 13:40
  • @Adil : ** while you have put this in your own constructor, so android never called application pre-defined constructor, so the context object of application class is null at that time,** Assuming this statement valid , i tried to call super() in constructor as first statement, and then assigning value to context, but still i am getting same exception. Note, now super class constructor is called so getApplicationContext should not return null now –  Dec 22 '12 at 13:57
  • It is because the Context is initialized first and later it is assigned to Application. [Have a look](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/app/Instrumentation.java#1034) here in the source code. – Adil Soomro Dec 22 '12 at 14:10
0

Your Application class lifecycle is quite similar to Activity's lifecycle. You do not need to care constructors, but instead, override already known onCreate() to do your object setup:

public class ContactManagerApplication extends Application {
    public static Context context = null;

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d("@gaurav", getApplicationContext()+"");
        context = getApplicationContext();
    }
}
Marcin Orlowski
  • 67,279
  • 10
  • 112
  • 132
0

Normally you have something more like this:

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
        //After this is when you normally would call the getContext
        Context myContext = this.getApplicationContext();
}

But this is only if this is the class that is being called. If this is not your class that is being called and is inside the class that is being called, then you can put the context as an instance variable and read it from there or you can send the context as a parameter in the constructor of this class and then use it. But if this is the case, then i dont see the point on this class being an extension of activity if is not called directly.

Jorge Aguilar
  • 3,392
  • 27
  • 34