有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java NullPointerException尝试从空对象引用上的字段纬度进行读取

我在安卓 Studio中使用Java开发了一个基于地图的安卓应用程序,用于查找,我尝试在地图上的两个标记之间创建一条多段线已有一段时间了,但此NullPointerException始终显示:

java.lang.NullPointerException: Attempt to read from field 'double com.google.安卓.gms.maps.model.LatLng.latitude' on a null object reference
 at com.example.waynedeng.mapx.Home.MapsActivity.getRequestUrl(MapsActivity.java:267)
 at com.example.waynedeng.mapx.Home.MapsActivity.polylineSetup(MapsActivity.java:261)
 at com.example.waynedeng.mapx.Home.MapsActivity.onMapReady(MapsActivity.java:180)
 at com.google.安卓.gms.maps.zzaj.zza(Unknown Source:7)
 at com.google.安卓.gms.maps.internal.zzaq.onTransact(Unknown Source:18)
 at 安卓.os.Binder.transact(Binder.java:627)
 at fg.b(:com.google.安卓.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):19)
 at com.google.安卓.gms.maps.internal.bg.a(:com.google.安卓.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):5)
 at com.google.maps.api.安卓.lib6.impl.be.run(:com.google.安卓.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):5)
 at 安卓.os.Handler.handleCallback(Handler.java:790)
 at 安卓.os.Handler.dispatchMessage(Handler.java:99)
 at 安卓.os.Looper.loop(Looper.java:164)
 at 安卓.app.ActivityThread.main(ActivityThread.java:6494)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
 at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:807)

以下是我的声明:

private GoogleMap mMap;
private Location lastKnownLocation;
private FusedLocationProviderClient client;
private ArrayList<LatLng> points;
private Handler handler;
private LatLng you = getLatLng(), destination = new LatLng(37.727812, -122.405864);
private LocationManager locationManager;
private List<Address> addresses;
private Geocoder geocoder;
private MarkerOptions youMarkerO, destinationMarkerO;
private Marker youMarker, destinationMarker;

错误位置似乎位于getDirections()方法中:

private String getDirections(String reqUrl) throws IOException {
    String response = "";
    InputStream inputStream = null;
    HttpURLConnection con = null;
    try {
        URL url = new URL(reqUrl);
        con = (HttpURLConnection) url.openConnection();
        con.connect();

        inputStream = con.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        StringBuffer stringBuffer = new StringBuffer();
        String line = ""; //ERROR
        while((line = bufferedReader.readLine()) != null){
            stringBuffer.append(line);
        }

        response = stringBuffer.toString();
        inputStreamReader.close();
        bufferedReader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if(inputStream !=null){
            inputStream.close();
        }
        con.disconnect();
    }
    return response;
}

在重写的onLocationChanged()方法中:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationListener = new LocationListener() {

        //Update location
        @Override
        public void onLocationChanged(Location location) {

            saveInfo();
            lastKnownLocation = location;
            Log.d(TAG, location.toString());
            Log.d(TAG, "Map cleared");

            mMap.clear();

            you = new LatLng(location.getLatitude(), location.getLongitude());

            mMap.getUiSettings().setZoomControlsEnabled(true);

            mMap.addMarker(youMarkerO);

            moveCamera(you, 0.5f);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

    };

下面是我的getRequestUrl()方法:

private String getRequestUrl(LatLng one, LatLng two){
    if( one != null && two != null){
        String origin = "origin="+ one.latitude + "," + one.longitude;
        String dest = "destination="+ two.latitude + "," + two.longitude;
        String sensor = "sensor=false";
        String mode = "mode=driving";
        String param = origin + "&" + dest + "&" + sensor + "&" + mode;
        String output = "json";
        String baseUrl = "https://maps.googleapis.com/maps/api/directions/";
        String requestUrl = baseUrl + output + "?" + param;
        return requestUrl;
    }
    return null;
}

起初,我认为这是因为我在初始化polylineSetup()之前访问了polylineSetup()中的“you”和“destination”,但我改变了这一点,它仍然不起作用。我试着解决它并读了这个问题:What is a NullPointerException, and how do I fix it?但它没有涵盖这个问题。任何信息或修复都会有所帮助。谢谢:)


共 (1) 个答案

  1. # 1 楼答案

    去排队前检查一下

    if( pickup.latitude != null &&  pickup.longitude != null && destination.latitude != null && destination.longitude != null)