title: NullPointerException for android ContextWrapper
body: <p>I have a class (DataSource.java) containing a method, which is calling another method in my MainActivity, which receives a context and a string variable. </p>
  
  <p>In the Datasource.java, when calling the method from the MainActivity, I have set the context variable to null, and even if I set it to "context" I receive the same error as I launch the app: EXCEPTION error for this line: <code>context = getApplicationContext();</code>.</p>
  
  <p>Could someone suggest me, where am I wrong?</p>
  
  <p>DataSource.java:</p>
  
  <pre><code>public class ToursDataSource {
  
   public boolean addToFollowList(Tour tour) {
      ContentValues values = new ContentValues();
      values.put(ToursDBOpenHelper.COLUMN_IMAGE, tour.getTitle());                
      long result = database.insert(ToursDBOpenHelper.TABLE_FOLLOW, null, values);
  
      MainActivity orgName = new MainActivity();
      orgName.sendRegId(this, "Test string");
  
      return null;        
   }
  }
  </code></pre>
  
  <p>MainActivity:</p>
  
  <pre><code>Context context;
      ...
  
          public void sendRegId(Context context, String organization) {
  
                   //PROBLEM IS AT THIS LINE
                  context = getApplicationContext();
  
                  // Check device for Play Services APK. If check succeeds, proceed with
                  //  GCM registration.
                  if (checkPlayServices()) {
                      gcm = GoogleCloudMessaging.getInstance(this);
                      regid = getRegistrationId(context);
                      Log.i(TAG, "Registration started!");
                      Log.d("RegisterActivity", "GCM RegId is: " + regid);           
  
                      if (regid.isEmpty()) {
  
                        registerInBackground();
                      }
                  } else {
                      Log.i(TAG, "No valid Google Play Services APK found.");
                  }
  
                  Sender results = new Sender();
                  results.shareRegIdWithAppServer(regid, organization);
  
  
              }
  
  
          ...
  </code></pre>
  
  <p>Logcat:</p>
  
  <pre><code>04-16 15:42:07.765: E/AndroidRuntime(15610): FATAL EXCEPTION: main
  04-16 15:42:07.765: E/AndroidRuntime(15610): java.lang.NullPointerException
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.exploreca.tourfinder.MainActivity.sendRegId(MainActivity.java:189)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.exploreca.tourfinder.ToursDataSource.addToFollowList(ToursDataSource.java:205)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.exploreca.tourfinder.TourDetailActivity.onOptionsItemSelected(TourDetailActivity.java:129)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.app.Activity.onMenuItemSelected(Activity.java:2640)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1171)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.view.View.performClick(View.java:4475)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.view.View$PerformClick.run(View.java:18786)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.os.Handler.handleCallback(Handler.java:730)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.os.Handler.dispatchMessage(Handler.java:92)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.os.Looper.loop(Looper.java:137)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.app.ActivityThread.main(ActivityThread.java:5493)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at java.lang.reflect.Method.invokeNative(Native Method)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at java.lang.reflect.Method.invoke(Method.java:525)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
  04-16 15:42:07.765: E/AndroidRuntime(15610):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>