Occasionally, from crash report, I can encounter Application instance is null, in my AppWidgetProvider.
This puzzles me quite some time, as I can never reproduce the same problem.
My setup is as following
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    ... >
    <application
        android:name=".JStockApplication"
        ... >
JStockApplication.java
public class JStockApplication extends android.support.multidex.MultiDexApplication  {
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    public void onCreate(){
        super.onCreate();
        me = this;
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        jStockOptions = JStockOptions.newInstance(sharedPreferences);
    }
    public static JStockApplication instance() {
        return me;
    }
    public JStockOptions getJStockOptions() {
        return jStockOptions;
    }
}
JStockAppWidgetProvider
public class JStockAppWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // context is not null. But, I'm not sure what is context.
        //
        // From crash report, JStockApplication.instance() can be null rarely.
        // JStockApplication.instance().getJStockOptions() will cause NPE.
    }
}
Very rarely, JStockApplication.instance().getJStockOptions() will cause NPE!
I realize most ppl (https://stackoverflow.com/a/5114361/72437) perform
me = this;
in onCreate
I'm not sure, whether performing such initialization (and PreferenceManager.getDefaultSharedPreferences(this)) can help to resolve such mystery crash.
Do you have any idea, why NPE happen?
 
    