title: google maps current location NullPointerException
body: <p><strong><em>Map problem</em></strong></p>
  
  <p>i have this code to get the current location and move map to that location
  but this code sometimes shows a location i was previously at and not currently at, and also for some reason it has now starting Force closing</p>
  
  <pre><code>private void initilizeMap() {
      if (googleMap == null) {
  
          FragmentManager fragmentManager = getSupportFragmentManager();
          SupportMapFragment mapFragment =  (SupportMapFragment)
              fragmentManager.findFragmentById(R.id.iqamah_map);
          googleMap = mapFragment.getMap();           
  
          googleMap.getUiSettings().setCompassEnabled(true);
          googleMap.getUiSettings().setZoomControlsEnabled(true);
          googleMap.getUiSettings().setMyLocationButtonEnabled(true);
          googleMap.getUiSettings().setZoomControlsEnabled(false);
          googleMap.setMyLocationEnabled(true); // false to disable
  
  
  
          // Getting LocationManager object from System Service LOCATION_SERVICE
          LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  
  
          // Creating a criteria object to retrieve provider
          Criteria criteria = new Criteria();
  
  
          // Getting the name of the best provider
          String provider = locationManager.getBestProvider(criteria, true);
  
  
          // Getting Current Location
          Location location = locationManager.getLastKnownLocation(provider);
  
          googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  
  //            CameraPosition cameraPosition = new CameraPosition.Builder().target(
  //                    new LatLng(17.385044, 78.486671)).zoom(12).build();
  
  //            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
  
          // Getting latitude of the current location
          double latitude = location.getLatitude();
  
          // Getting longitude of the current location
          double longitude = location.getLongitude();
  
          url = "https://codingsquared-prod.apigee.net/masaajid?lat="+Double.toString(latitude)+"&amp;long="+Double.toString(longitude);
  
          // Creating a LatLng object for the current location
          LatLng latLng = new LatLng(latitude, longitude);
  
          TextView txtview = (TextView) findViewById(R.id.iqamah_listview_text);
          txtview.setText("latitude="+Double.toString(latitude)+" longitude="+Double.toString(longitude));
  
          // Showing the current location in Google Map
          googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  
          // Zoom in the Google Map
          googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
  
  
      }
  
  }
  </code></pre>
  
  <p>logcat : </p>
  
  <pre><code>06-28 01:05:01.554: E/AndroidRuntime(13477): FATAL EXCEPTION: main
  06-28 01:05:01.554: E/AndroidRuntime(13477): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sahaab.android.muslimzone.test/com.sahaab.muslimzone.activity.IqamahActivity}: java.lang.NullPointerException
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.os.Handler.dispatchMessage(Handler.java:99)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.os.Looper.loop(Looper.java:213)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.app.ActivityThread.main(ActivityThread.java:5225)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at java.lang.reflect.Method.invokeNative(Native Method)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at java.lang.reflect.Method.invoke(Method.java:525)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at dalvik.system.NativeStart.main(Native Method)
  06-28 01:05:01.554: E/AndroidRuntime(13477): Caused by: java.lang.NullPointerException
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at com.sahaab.muslimzone.activity.IqamahActivity.initilizeMap(IqamahActivity.java:155)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at com.sahaab.muslimzone.activity.IqamahActivity.onCreate(IqamahActivity.java:103)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.app.Activity.performCreate(Activity.java:5133)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
  06-28 01:05:01.554: E/AndroidRuntime(13477):    ... 12 more
  </code></pre>
  
  <p>also tried for current location : </p>
  
  <pre><code>        Location myLocation = googleMap.getMyLocation();
          LatLng myLatLng = new LatLng(myLocation.getLatitude(),
                  myLocation.getLongitude());
  
          CameraPosition myPosition = new CameraPosition.Builder()
                  .target(myLatLng).zoom(17).bearing(90).tilt(30).build();
          googleMap.animateCamera(
              CameraUpdateFactory.newCameraPosition(myPosition));
  </code></pre>