title: PreferenceManager.getDefaultSharedPreferences(getContext()) is null
body: <p>I'm trying to create an intercom app, but when I try to run it, it keeps giving me an error which is <code>getContext() is null</code>.<br>
  Can anyone help me with this?</p>
  
  <pre><code>public class intercomApp extends Application {
  
  protected static final String SHARED_PREFS_REFERENCE_LABEL = "ComeNChat";
  
  protected static final String DEFAULT_NAME = "Default_Name";
  
  private static intercomApp instance;
  
  public static intercomApp getInstance() {
      return instance;
  }
  
  public static Context getContext() {
      return instance;
  }
  
  @Override
  public void onCreate() {
      instance = this;
      super.onCreate();
  }
  
  // store our name to shared prefs
  public static void storeName(String name) {
      PreferenceManager.getDefaultSharedPreferences(getContext()).edit().putString(SHARED_PREFS_REFERENCE_LABEL, name).commit();
  }
  
  // get our name from shared prefs
  public static String getName() {
      return PreferenceManager.getDefaultSharedPreferences(getContext()).getString(SHARED_PREFS_REFERENCE_LABEL, DEFAULT_NAME); //(intercomApp.java:45)
  }
  </code></pre>
  
  <p>This is my logcat</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ricky.comenchat/com.example.ricky.comenchat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5466)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                                                                               at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375)
                                                                               at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370)
                                                                               at com.example.ricky.comenchat.util.intercomApp.getName(intercomApp.java:45)
                                                                               at com.example.ricky.comenchat.MainActivity.getClientNameList(MainActivity.java:383)
                                                                               at com.example.ricky.comenchat.MainActivity.setupClientListAdapter(MainActivity.java:214)
                                                                               at com.example.ricky.comenchat.MainActivity.onCreate(MainActivity.java:149)
                                                                               at android.app.Activity.performCreate(Activity.java:6251)
  </code></pre>
  
  <p>While this is the function used in MainActivity</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  @Overide
  protected void onCreate(Bundle savedInstanceState){
       setupList; //(MainActivity.java:149)
  }
  
  private void setupList{
       nameList = getNamelist(); //(MainActivity.java:214)
  }
  
  private String[] getNamelist(){
       ArrayList&lt;String&gt; names = new ArrayList&lt;&gt;();
       names.add(intercomApp.getName()); //(MainActivity.java:383)
  }
  </code></pre>
  
  <p>EDITED:
  Below is my manifest.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.ricky.comenchat"&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="android.permission.NETWORK" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
  
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>