0

I am trying to create an social application which allow users to post something fetching their location and show that post only to the nearest users. However, i am getting NPE error while fetching the longitude and latitude of user. I couldnot figureout what actually is wrong with my code. Here is the error:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
        at com.jstechnologies.helpinghands.utils.AddressUtils.getAddressFromGps(AddressUtils.java:44)
        at com.jstechnologies.helpinghands.ui.MyApp.getAddress(MyApp.java:30)
        at com.jstechnologies.helpinghands.ui.views.dashBoard.DashBoardViewModel.fetchAddress(DashBoardViewModel.java:65)
        at com.jstechnologies.helpinghands.ui.views.dashBoard.DashBoardActivity.onCreate(DashBoardActivity.java:64)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)

This is my AddressUtils.java file

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class AddressUtils {
    private static final int RC_LOCATION_PERMISSION = 102;

    @AfterPermissionGranted(RC_LOCATION_PERMISSION)
    public Address getAddressFromGps(Context context) throws IOException {

        String[] perms = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
        if (EasyPermissions.hasPermissions(context, perms)) {
            String provider;
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);


            @SuppressLint("MissingPermission")
            Location location = locationManager.getLastKnownLocation(provider);
            Geocoder geocoder= new Geocoder(context, Locale.getDefault());
            List<android.location.Address> addresses  = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(), 1);
            Address address= new Address();

//ACCORDING TO LOGCAT THIS BELOW LINE HAS ERROR I GUESS

            address.setLat(location.getLatitude());
            address.setLon(location.getLongitude());
            address.setCountry(addresses.get(0).getCountryName());
            address.setCity(addresses.get(0).getLocality());
            address.setState(addresses.get(0).getAdminArea());
            address.setPincode(addresses.get(0).getPostalCode());
            address.setAddressLine1(addresses.get(0).getAddressLine(0));
            address.setAddressLine2(addresses.get(0).getAddressLine(1));

            return address;
        } else {
            EasyPermissions.requestPermissions((Activity) context,"Location permissions are required",RC_LOCATION_PERMISSION,perms);
            return null;
        }

    }
}

I would like to add some images of the line of code regarding the error shown by logcat on other java files (maybe this isn't required). I need to complete this project in 2 days , however, i am stucked here. Any help please.

MyApp.java
DashboardViewModel.java
DashboardActivity.java

a_local_nobody
  • 7,360
  • 5
  • 25
  • 45
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Jul 05 '21 at 11:38

0 Answers0