1

I'm quite new to the android and I've got this project I'm working on. However, I have encountered a bit of a challenge here.. The app is supposed to draw a line from the user's current location to a selected destination. Instead, when a destination is selected, it throws this error..

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

Below is the code block

public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response.body());
                        JSONArray jsonArray = jsonObject.getJSONArray("routes");

                        for(int i=0;i<jsonArray.length();i++)
                        {
                            JSONObject route = jsonArray.getJSONObject(i);
                            JSONObject poly = route.getJSONObject("overview_polyline");
                            String polyline = poly.getString("points");
                            polyLineList = decodePoly(polyline);

                        }

                        LatLngBounds.Builder builder = new LatLngBounds.Builder();
                        for(LatLng latLng:polyLineList)
                            builder.include(latLng);
                        LatLngBounds bounds = builder.build();
                        CameraUpdate mCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds,2);
                        mMap.animateCamera(mCameraUpdate);

                        polylineOptions = new PolylineOptions();
                        polylineOptions.color(Color.GRAY);
                        polylineOptions.width(5);
                        polylineOptions.startCap(new SquareCap());
                        polylineOptions.endCap(new SquareCap());
                        polylineOptions.jointType(JointType.ROUND);
                        polylineOptions.addAll(polyLineList);
                        greyPolyline = mMap.addPolyline(polylineOptions);


                        blackPolylineOptions = new PolylineOptions();
                        blackPolylineOptions.color(Color.BLACK);
                        blackPolylineOptions.width(5);
                        blackPolylineOptions.startCap(new SquareCap());
                        blackPolylineOptions.endCap(new SquareCap());
                        blackPolylineOptions.jointType(JointType.ROUND);
                        blackPolyline = mMap.addPolyline(blackPolylineOptions);

                        mMap.addMarker(new MarkerOptions()
                        .position(polyLineList.get(polyLineList.size()-1))
                        .title("Pick a location"));


                        //animations
                        ValueAnimator polyLineAnimator = ValueAnimator.ofInt(0,100);
                        polyLineAnimator.setDuration(2000);
                        polyLineAnimator.setInterpolator(new LinearInterpolator());
                        polyLineAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                List<LatLng> points = greyPolyline.getPoints();
                                int percentValue = (int)animation.getAnimatedValue();
                                int size = points.size();
                                int newPoints = (int) (size * (percentValue/100.0f));
                                List<LatLng> p = points.subList(0,newPoints);
                                blackPolyline.setPoints(p);

                            }
                        });

                        polyLineAnimator.start();

                        carMarker = mMap.addMarker(new MarkerOptions().position(currentPoint)
                        .flat(true)
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));

                        handler = new Handler();
                        index =-1;
                        next=1;
                        handler.postDelayed(drawPathRunnable,3000);

                    }
                    catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                }
coder
  • 8,346
  • 16
  • 39
  • 53
  • https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it Based on the code and the error, jsonArray is null, likely because there is no node routes in the JSON you got in the response. – Rick Sanchez May 06 '18 at 09:10
  • 1
    Possible duplicate of [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) – Henry May 06 '18 at 09:10
  • can you post your logcat error? – Mohamed Nageh May 06 '18 at 09:11
  • @Henry, You comment is possibly a duplicate as well hahahahaha. how meta :P – Rick Sanchez May 06 '18 at 09:16
  • @RickSanchez it is an automatically added comment when you vote to close as a duplicate. – Henry May 06 '18 at 09:21
  • which line are you getting this error in or is it in the google code? – Rishabh Jain May 06 '18 at 09:56
  • @RickSanchez this one: JSONObject route = jsonArray.getJSONObject(i); – Emmanuel Asante May 06 '18 at 13:10
  • I just realized this...if i try to enter the destination, the url passed is this: https://googleapis.com/maps/api/directions/json?mode=driving&transit_routing_preference=less_driving&origin=5.2887514,-2.0000966&destination=Tarkwa,+Ghana&key=AIzaSyDsGo4ebhi8GBv5NpZvbq-9DdEoL_wurdw. You'll notice that 'destination=Tarkwa'. This is not the expected result because Tarkwa is a town. I expect a location in this town (University of Mines and Technology UMaT) to be passed to the destination variable and not the town – Emmanuel Asante May 06 '18 at 13:28

0 Answers0