0

I want to use GPS to find Lattitude and Longitude and I want to parse it to a JSONObject. But while doing so, I get a null pointer exception for

JSONObject jsonObject = new JSONObject(response.body().toString());

rdiShipToAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        mGoogleMapService.getAddressName(String.format("https://maps.googleapis.com/maps/api/geocode/json?latlng= %f, %f & sensor= false",
                                mLastLocation.getLatitude(),
                                mLastLocation.getLongitude()))
                                .enqueue(new Callback<String>() {
                            @Override
                            public void onResponse(Call<String> call, Response<String> response) {

                                try {

                                    JSONObject jsonObject = new JSONObject(response.body().toString());

                                    JSONArray resultsArray = jsonObject.getJSONArray("results");

                                    JSONObject firstObject = resultsArray.getJSONObject(0);

                                    address = firstObject.getString("formatted_address");

                                    //Set this address to edtAddress
                                    ((EditText)edtAddress.getView().findViewById(R.id.place_autocomplete_search_input))
                                            .setText(address);




                                } catch (JSONException e) {
                                    e.printStackTrace();

                                }

                            }

                            @Override
                            public void onFailure(Call<String> call, Throwable t) {
                                MDToast mwdqToaswuewt = MDToast.makeText(getBaseContext(),""+t.getMessage(),Toast.LENGTH_SHORT,MDToast.TYPE_WARNING);
                                mwdqToaswuewt.show();
                            }
                        });

                    }
                }
            });

This is error message.

E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
    at com.cyber.eatit.Cart$1$5$1.onResponse(Cart.java:373)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5568)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cyber.eatit, PID: 3362
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
    at com.cyber.eatit.Cart$1$5$1.onResponse(Cart.java:373)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5568)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

I suspect on API link ,

String.format("https://maps.googleapis.com/maps/api/geocode/json?latlng= **%f**, **%f** & sensor= false"

Because lattitude and longitude doesnt get written, from here.

Is this API link right, because this is how it looks, is a comma that separates Lat and Lng a problem here_

https://maps.googleapis.com/maps/api/geocode/json?latlng=53,090298**,**8,774519&sensor=false}
Sajith
  • 630
  • 4
  • 20

2 Answers2

0

Did you try looking on the Developer's Guide?

https://developers.google.com/maps/documentation/geocoding/intro

on Example of Reverse Geocoding

Sample is: https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY

Jayson Garcia
  • 164
  • 3
  • 14
0

Last known location has the potential to return null location if GPS has out of date location data or has been out of connection with GPS network for a while.

Check first if last known location is actually not null and then send it. If it is null, you might have to listen to the actual current location updates using Fused Location provider.

Also make sure that if you use android sdk 23+, you will have to explicitly ask for permissions to listen to location updates or you will get a security exception

Kushan
  • 5,275
  • 2
  • 27
  • 41