title: Android Logcat Error: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
body: <p>Good day!</p>
  
  <p>I am currently experiencing a dilemma where my code works on one of my Android devices but force closes on the other one.</p>
  
  <p>Based on the my logcat, this is the error:</p>
  
  <pre><code>02-27 06:37:29.134 23579-23579/com.example.maui.dabtrain E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
                                                                                at com.example.maui.dabtrain.MainActivity.onMapReady(MainActivity.java:504)
                                                                                at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
                                                                                at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
                                                                                at android.os.Binder.transact(Binder.java:507)
                                                                                at com.google.android.gms.maps.internal.bw.a(:com.google.android.gms.DynamiteModulesB:82)
                                                                                at com.google.maps.api.android.lib6.impl.bf.run(:com.google.android.gms.DynamiteModulesB:1805)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  </code></pre>
  
  <p>Here is the code for my location:</p>
  
  <pre><code>        gMap = googleMap;
  
          if(!gMap.isMyLocationEnabled())
              gMap.setMyLocationEnabled(true);
  
          LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
          Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  
  
          if (myLocation == null) {
              Criteria criteria = new Criteria();
              criteria.setAccuracy(Criteria.ACCURACY_COARSE);
              String provider = lm.getBestProvider(criteria, true);
              myLocation = lm.getLastKnownLocation(provider);
          }
  
  
      LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
      gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 13), 1500, null);
  </code></pre>
  
  <p>The rest of the code is for Google Markers, Polyline, Radius, etc.</p>
  
  <p>Thank you for your time!</p>