Getting results of nearby places from the current Location using Google Maps API

Solution for Getting results of nearby places from the current Location using Google Maps API
is Given Below:

Here is my code:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,
        LocationListener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

GoogleMap mMap;
SupportMapFragment mapFragment;
LocationRequest mLocationRequest;
GoogleApiClient client;
Location mLastLocation;
Marker mCurrLocationMarker;
LatLng latLongCurrent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_map);

    getSupportActionBar().setTitle("Map location");
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

public void findRestaurant(View view) {
    StringBuilder stringBuilder = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
    stringBuilder.append("location=" + latLongCurrent.latitude + "," + latLongCurrent.longitude); //error attempt to read from field 'double...on a null object reference
    stringBuilder.append("&radius=" + 1000);
    stringBuilder.append("&keyword=" + "restaurant");
    stringBuilder.append("&key=" + getResources().getString(R.string.google_places_key));

    String url = stringBuilder.toString();

    Object dataTransfer[] = new Object[2];
    dataTransfer[0] = mMap;
    dataTransfer[1] = url;

    NearbySearch nearbySearch = new NearbySearch(this);
    nearbySearch.execute(dataTransfer);
}

@Override
public void onPause() {
    super.onPause();

    //stop location updates when Activity is no longer active
    if (client != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.map_options, menu);
    return true;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    //Initializing the Google Play Services
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            //Location Permission already granted
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        } else {
            //Request Location Permission
            checkLocationPermission();
        }
    } else {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    }
}

protected synchronized void buildGoogleApiClient() {
    client = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    client.connect();
}

@Override
public void onConnected(Bundle bundle) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(client, mLocationRequest, this);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Change the map type based on the user's selection.
    switch (item.getItemId()) {
        case R.id.normal_map:
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            return true;
        case R.id.hybrid_map:
            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            return true;
        case R.id.satellite_map:
            mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            return true;
        case R.id.terrain_map:
            mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("My current Position");
    
    mCurrLocationMarker = mMap.addMarker(markerOptions);
}

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

private void checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {

            new AlertDialog.Builder(MapsActivity.this)
                    .setTitle("Location Permission Needed")
                    .setMessage("This app needs the Location permission, please accept this to use the location functionality")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //Prompt the user once explanation has been shown
                            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
                        }
                    }).create().show();

        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {

                    if (client == null) {
                        buildGoogleApiClient();
                    }
                    mMap.setMyLocationEnabled(true);
                }

            } else {

                Toast.makeText(this, "Permission is denied", Toast.LENGTH_LONG).show();
            }
            return;
        }

    }
}

private void moveCamera(LatLng latLang, float zoom, String title) {
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLang, zoom));

    //writing the code to drop the pin or marker
    MarkerOptions options = new MarkerOptions().position(latLang).title(title);
    mMap.addMarker(options);
}


@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

and this is my class NearbySearch.java code below:

public class NearbySearch extends AsyncTask<Object, String, String> {
GoogleMap mMap;
String url;
InputStream is;
BufferedReader bufferedReader;
StringBuilder stringBuilder;
String data;

public NearbySearch(MapsActivity mapsActivity) {
}

@Override
protected String doInBackground(Object... objects) {

    //requesting for the response from the google places API
    mMap = (GoogleMap) objects[0];
    try {
        URL myUrl = new URL(url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) myUrl.openConnection();
        is = httpURLConnection.getInputStream();
        bufferedReader = new BufferedReader(new InputStreamReader(is));

        String line = "";
        stringBuilder = new StringBuilder();

        while ((line = bufferedReader.readLine()) != null) {

            stringBuilder.append(line);
        }

        //Receiving the data from json
        data = stringBuilder.toString();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return data;
}

@Override
protected void onPostExecute(String s) {

    try {
        JSONObject parentObject = new JSONObject(s);
        JSONArray resultArray = parentObject.getJSONArray("results");

        for (int i = 0; i < resultArray.length(); i++) {
            JSONObject jsonObject = resultArray.getJSONObject(i);
            JSONObject locationObject = jsonObject.getJSONObject("geometry").getJSONObject("location");

            String latitude = jsonObject.getString("lat");
            String longitude = jsonObject.getString("lng");

            JSONObject nameObject = resultArray.getJSONObject(i);

            String nameRestaurant = nameObject.getString("name");
            String vicinity = nameObject.getString("vicinity");

            LatLng latLng = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));

            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.title(vicinity);
            markerOptions.position(latLng);

            mMap.addMarker(markerOptions);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    super.onPostExecute(s);
}
}

I’m successfully fetching the user’s current location but cannot find the solution to find the nearby restaurants from the current location. Please help to get rid of it as I’m stuck for 2 days here. Kindly tell me if any other solution is there too. Thanks!