I need the program to show my location in the logs every 0.1 seconds.

mMap.setMyLocationEnabled(true); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); final Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); for (int i = 1; i < 100; i++) new Handler().postDelayed(new Runnable() { @Override public void run() { if (ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } mMap.setMyLocationEnabled(true); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); final Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); Log.d("Coordinates: ", location.getLongitude() + "," + location.getLatitude()); } }, 100 * i); 

The program runs through the emulator, coordinates appear in the logs, but when I turn it on via the phone, the program crashes in the line where the logs are, with the following error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference 
  • the documentation says the following - If the provider is currently disabled, null is returned. - ermak0ff
  • How can this be fixed? - Hayk Abelyan
  • Well, at a minimum, including location determination and the corresponding provider - ermak0ff
  • The phone has location detection enabled. Is it about an internet provider? - Hayk Abelyan
  • Coordinate provider may be: cell phone towers, wifi, gps ... - ermak0ff

0