原因:java.lang.nullpointerexception:尝试对空对象引用调用虚拟方法“double android.location.location.getlatitude()”

eaf3rand  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(241)

我正在尝试创建一个社交应用程序,允许用户发布获取其位置的内容,并仅向最近的用户显示该内容。然而,在获取用户的经度和纬度时,我得到了npe错误。我不知道我的代码到底出了什么问题。以下是错误:

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)

这是我的addressutils.java文件

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;
        }

    }
}

我想添加一些关于logcat在其他java文件上显示的错误的代码行图像(可能这不是必需的)。我需要在2天内完成这个项目,然而,我在这里结巴。请帮忙。
myapp.java
dashboardviewmodel.java
dashboardactivity.java

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题