-1

'double android.location.Location.getLatitude()' on a null object reference

 return new ParseGeoPoint(loc.getLatitude(), loc.getLongitude());

and with the ParseGeoQuery

query.whereWithinKilometers("GeoArea", geoPointFromLocation(myLoc), radius);

This is the onCreate Method where the location is being initialized

   // Create a new global location parameters object
        locationRequest = LocationRequest.create();

        // Set the update interval
        locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

        // Use high accuracy
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        // Set the interval ceiling to one minute
        locationRequest.setFastestInterval(FAST_INTERVAL_CEILING_IN_MILLISECONDS);

        // Create a new location client, using the enclosing class to handle callbacks.
        locationClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

This is my ParseQuery for GeoLocation

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

        // Connect to the location services client
        locationClient.connect();
    }
     FiltersQueryAdapter mainAdapter = new FiltersQueryAdapter(this, PhotoFiltersAdapter.class
                , new ParseRecyclerQueryAdapter.QueryFactory() {
            public ParseQuery create() {
                Location myLoc = (currentLocation == null) ? lastLocation : currentLocation;
                ParseQuery query = ParseQuery.getQuery("PlaceFilters");
                //query.include("user");
                query.orderByAscending("GeoArea");
                query.whereWithinKilometers("GeoArea", geoPointFromLocation(myLoc), radius);
                query.setLimit(6);
                return query;
            }
        });

     @Override
        protected void onResume() {
            super.onResume();
            cameraView.onResume();

            // Get the latest search distance preference
            radius = InstaMaterialApplication.getSearchDistance();
            // Checks the last saved location to show cached data if it's available
            if (lastLocation != null) {
                // If the search distance preference has been changed, move
                // map to new bounds.
                if (lastRadius != radius) {
                    // Save the current radius
                    lastRadius = radius;
                    doListQuery();
                }
            }
        }

        public void onConnected(Bundle bundle) {
                if (InstaMaterialApplication.APPDEBUG) {
                    Log.d("Location Connected", InstaMaterialApplication.APPTAG);
                }
                currentLocation = getLocation();
                startPeriodicUpdates();
            }

          public void onLocationChanged(Location location) {
                currentLocation = location;
                if (lastLocation != null
                            && geoPointFromLocation(location)
                            .distanceInKilometersTo(geoPointFromLocation(lastLocation)) < 0.01)
                {
                    // If the location hasn't changed by more than 10 meters, ignore it.
                    return;
                }
                lastLocation = location;

                if (!hasSetUpInitialLocation) {
                    hasSetUpInitialLocation = true;
                }
                // Update map radius indicator
                doListQuery();
            }

            /*
           * In response to a request to start updates, send a request to Location Services
           */
            private void startPeriodicUpdates() {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(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;
                }
                LocationServices.FusedLocationApi.requestLocationUpdates(
                        locationClient, locationRequest, this);
            }

            /*
             * In response to a request to stop updates, send a request to Location Services
             */
            private void stopPeriodicUpdates() {
                locationClient.disconnect();
            }

            /*
           * Get the current location
           */
            private Location getLocation() {
                // If Google Play Services is available
                if (servicesConnected()) {
                    // Get the current location
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(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 LocationServices.FusedLocationApi.getLastLocation(locationClient);
                } else {
                    return null;
                }
            }

            /*
           * Set up a query to update the list view
           */
            private void doListQuery() {
                Location myLoc = (currentLocation == null) ? lastLocation : currentLocation;
                // If location info is available, load the data
            }

            /*
           * Helper method to get the Parse GEO point representation of a location
           */
            private ParseGeoPoint geoPointFromLocation(Location loc) {
                return new ParseGeoPoint(loc.getLatitude(), loc.getLongitude());
            }

My Manifest Permissions to access Google Play Services and Location

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
Sagar Atalatti
  • 486
  • 1
  • 8
  • 21
  • Value of loc must be null(It must have not got any value) – Shashank Udupa Feb 17 '16 at 10:45
  • I haven't called it getLatitude elsewhere other than in ParseGeoPoint GeoPointfromLocation – Sagar Atalatti Feb 17 '16 at 11:11
  • @saeed should I add this double latitude = location.getLatitude(); below lastLocation = location – Sagar Atalatti Feb 17 '16 at 11:27
  • @saeed I get Null on loc.getLatitude and get loc.getLongitude as I have posted it on the top of my post. And can also you find it in ParseGeoQuery section it has been posted in the bottom – Sagar Atalatti Feb 17 '16 at 11:40
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Feb 18 '16 at 20:38

1 Answers1

0

Add following another permission in your manifest

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

I know you are using ACCESS_FINE_LOCATION, but what if you lost GPS signal and try to get lat long. In this scenario you will get null data. To avoid such situation better to have another option to have location from Network Provider though it may not be accurate.

Make sure to connect your googleApiClient with this call after your client builder.

// Create a new location client, using the enclosing class to handle callbacks.
        locationClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

locationClient.connect();
Rakesh
  • 756
  • 1
  • 9
  • 19