Android Mobile App

I am currently working on a project that requires me to get the address using reverse geocode. I am able to get the data from the URL but keep getting an error as follows. Would be great if i can get a solution to this problem. Thanks!

“12-19 03:01:54.419 5088-5088/com.junjie.retrofit_location E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.junjie.retrofit_location, PID: 5088
java.lang.NullPointerException: Attempt to invoke virtual method ‘int java.util.ArrayList.size()’ on a null object reference
at com.junjie.retrofit_location.MainActivity$1.onResponse(MainActivity.java:61)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)”

Hi JunJie,

This error message is telling you that the object handled is empty/null (Nothing inside the arraylist). To handle this exception, you will need a try-catch-finally statement.

Regards,
Kai Cong

Hi Kaicong,
Okay will try out the try/catch method, but just wondering is there any solution to retrieve the json under the GeocodeInfo as I’m using retrofit to get the response but keep getting the null object error. Is there a way to get the object such as the buildingname, block, road, etc.

image

Regards,
Jun Jie

Hi Jun Jie,

There is a chance for you to retrieve an empty object because some locations do not retrieve any feature information. I am afraid you will have to handle it with appropriate message like “No Location Info” or giving back their LatLng position.

Hope that clarifies.

Regards,
Kai Cong