title: GoogleAPIclient NullPointerException
body: <p>I am running trying to get user current location in AsyncTask and transfer Location object as return value back to calling activity(Which contains Latitude,Longitude etc).</p>
  
  <p>But whenever i call Asynctask from my activity like this</p>
  
  <pre><code>new AsyncTask_GetLocation(Navigation_Drawer.this,location_interface,1,1,LocationRequest.PRIORITY_HIGH_ACCURACY).execute();
  </code></pre>
  
  <p>Above line will start AsyncTask for getting location.</p>
  
  <pre><code>public class AsyncTask_GetLocation extends AsyncTask&lt;Void, Void, Location&gt; implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
      Context context;
      GoogleApiClient LocationapiClient;
      LocationRequest mLocationRequest;
      Location mCurrentLocation;
      LocationManager locationManager;
      Location_Interface locationInterface;
      int Interval;
      int Fastest;
      int Priority;
  
      public static final String TAG="###GET LOCATION###";
  
      public AsyncTask_GetLocation(Context context,Location_Interface location_interface, int interval, int fastest, int priority) {
          this.context = context;
          Interval = interval;
          Fastest = fastest;
          Priority = priority;
          locationInterface=location_interface;
      }
  
      @Override
      protected Location doInBackground(Void... params) {
          Log.d(TAG,"Do in Background Running "+context);
          GoogleAPI(context);
          LocationapiClient.connect();
          if (mCurrentLocation!=null){
              Log.d(TAG,"Current Location Is Not Null "+mCurrentLocation);
              return mCurrentLocation;
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(Location location) {
          super.onPostExecute(location);
          locationInterface.userCurrent_Location(location);
          LocationServices.FusedLocationApi.removeLocationUpdates(LocationapiClient,this);
  
      }
  
      @Override
      public void onConnected(@Nullable Bundle bundle) {
          Log.d(TAG, "ON CONNECTED CALLED GOOGLE API CONNECTED ");
          if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
              return;
          }
          LocationServices.FusedLocationApi.requestLocationUpdates(LocationapiClient, mLocationRequest, this);
      }
  
      @Override
      public void onConnectionSuspended(int i) {
  
      }
  
      @Override
      public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  
      }
  
      @Override
      public void onLocationChanged(Location location) {
          Log.d(TAG,"On Location Changed");
          mCurrentLocation=location;
      }
      private void requestLocationUpdate(int Interval,int Fastest,int Priority) {
          Log.d(TAG, "Request Location Update Calling");
          mLocationRequest = new LocationRequest();
          mLocationRequest.setInterval(Interval);
          mLocationRequest.setFastestInterval(Fastest);
          mLocationRequest.setPriority(Priority);
      }
  
      public synchronized void GoogleAPI(Context apiContext){
          requestLocationUpdate(Interval,Fastest,Priority);
          Log.d(TAG,"Location Api Client Value "+LocationapiClient);
          if (LocationapiClient==null){
              LocationapiClient = new GoogleApiClient.Builder(apiContext) // THIS LINE THROW NPE  
                      .addApi(LocationServices.API)
                      .addConnectionCallbacks(this)
                      .addOnConnectionFailedListener(this)
                      .build();
          }
  
          LocationapiClient.connect();
      }
  }
  </code></pre>
  
  <p>LogCat</p>
  
  <pre><code>04-26 18:33:18.325 15252-15725/com.example.com.Loc D/###GET LOCATION###: Location Api Client Value null
  04-26 18:33:18.325 15252-15725/com.example.com.Loc W/dalvikvm: threadid=21: thread exiting with uncaught exception (group=0x40d53378)
  04-26 18:33:18.425 15252-15725/com.example.com.Loc E/UncaughtException: java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                        at android.os.AsyncTask$3.done(AsyncTask.java:299)
                                                                                        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
                                                                                        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
                                                                                        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
                                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
                                                                                        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
                                                                                        at java.lang.Thread.run(Thread.java:856)
                                                                                     Caused by: java.lang.NullPointerException
                                                                                        at android.content.ContextWrapper.getMainLooper(ContextWrapper.java:96)
                                                                                        at com.google.android.gms.common.api.GoogleApiClient$Builder.&lt;init&gt;(Unknown Source)
                                                                                        at com.example.com.Loc.Location_system.AsyncTask_GetLocation.GoogleAPI(AsyncTask_GetLocation.java:102)
                                                                                        at com.example.com.Loc.Location_system.AsyncTask_GetLocation.doInBackground(AsyncTask_GetLocation.java:48)
                                                                                        at com.example.com.Loc.Location_system.AsyncTask_GetLocation.doInBackground(AsyncTask_GetLocation.java:24)
                                                                                        at android.os.AsyncTask$2.call(AsyncTask.java:287)
                                                                                        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
                                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
                                                                                        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
                                                                                        at java.lang.Thread.run(Thread.java:856) 
  04-26 18:33:18.695 15252-15334/com.example.com.Loc I/FA: Tag Manager is not found and thus will not be used
  04-26 18:33:18.785 15252-15334/com.example.com.Loc D/FA: Logging event (FE): _ae, Bundle[{_o=crash, timestamp=1493211798437, fatal=1}]
  04-26 18:33:18.935 15252-15334/com.example.com.Loc D/FA: Connected to remote service
  04-26 18:33:19.335 15252-15725/com.example.com.Loc E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
                                                                                 java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                     at android.os.AsyncTask$3.done(AsyncTask.java:299)
                                                                                     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
                                                                                     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
                                                                                     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
                                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
                                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
                                                                                     at java.lang.Thread.run(Thread.java:856)
                                                                                  Caused by: java.lang.NullPointerException
                                                                                     at android.content.ContextWrapper.getMainLooper(ContextWrapper.java:96)
                                                                                     at com.google.android.gms.common.api.GoogleApiClient$Builder.&lt;init&gt;(Unknown Source)
                                                                                     at com.example.com.Loc.Location_system.AsyncTask_GetLocation.GoogleAPI(AsyncTask_GetLocation.java:102)
                                                                                     at com.example.com.Loc.Location_system.AsyncTask_GetLocation.doInBackground(AsyncTask_GetLocation.java:48)
                                                                                     at com.example.com.Loc.Location_system.AsyncTask_GetLocation.doInBackground(AsyncTask_GetLocation.java:24)
                                                                                     at android.os.AsyncTask$2.call(AsyncTask.java:287)
                                                                                     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
                                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
                                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
                                                                                     at java.lang.Thread.run(Thread.java:856) 
  04-26 18:33:19.525 15252-15252/com.example.com.Loc D/###Navigation Drawer###: ON PAUSE CALLING
  </code></pre>