1

I have been working on a map based android app in Android Studio using Java for finding and I've been trying to create a polyline in between two markers on the map for a while but this NullPointerException keeps showing:

java.lang.NullPointerException: Attempt to read from field 'double com.google.android.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.android.gms.maps.zzaj.zza(Unknown Source:7)
 at com.google.android.gms.maps.internal.zzaq.onTransact(Unknown Source:18)
 at android.os.Binder.transact(Binder.java:627)
 at fg.b(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):19)
 at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):5)
 at com.google.maps.api.android.lib6.impl.be.run(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):5)
 at android.os.Handler.handleCallback(Handler.java:790)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:164)
 at android.app.ActivityThread.main(ActivityThread.java:6494)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Here are my declarations:

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;

The error location seems to be in the getDirections() method:

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

And in the overrided onLocationChanged() method:

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) {

        }

    };

And here is my getRequestUrl() method:

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

At first, I thought it was because I accessed "you" and "destination" in the polylineSetup() before it was initialized but I changed that and it still doesn't work. I tried to solve it and read this question: What is a NullPointerException, and how do I fix it? but it didn't cover the problem. Any info or fixes would help. Thanks :)

Ign1s Shotta
  • 85
  • 2
  • 12

1 Answers1

0

check before going to make a line

if( pickup.latitude != null &&  pickup.longitude != null && destination.latitude != null && destination.longitude != null)
Manivash
  • 38
  • 6