title: Getting error in the location manager
body: <p>I am trying to find out current latitude and longitude of android device but I am getting java.lang.nullpointerexception and app is force closing.even though i get refrence from here and used the code from the answer which was accepted as correct:
  <a href="https://stackoverflow.com/questions/16498450/how-to-get-android-gps-location">How to get Android GPS location</a>
  my code is:</p>
  
  <pre><code>try{
              LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
               Criteria criteria = new Criteria();
               String bestProvider = locationManager.getBestProvider(criteria, false);
               Location location = locationManager.getLastKnownLocation(bestProvider);
  
  
          double latit=location.getLatitude();
          double longit=location.getLongitude();
          Toast.makeText(getApplicationContext(),"Your Location is:\nLatitude:\t"+latit+"\nLongitude:\t"+longit,Toast.LENGTH_LONG).show();
              }catch(Exception ex){
                  Toast.makeText(getApplicationContext(), "Error:"+ex,Toast.LENGTH_LONG).show();
              }
  </code></pre>