-1

I had tried doing this prior to this without the library and it worked fine. However using the retrofit library i am getting it returned this log:

FATAL EXCEPTION: main Process: com.example.lenovo.retrof, PID: 21218 java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.lenovo.retrof.POJO.MainResponse.getResults()' on a null object reference at com.example.lenovo.retrof.MainActivity$1.onResponse(MainActivity.java:53) 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:207) at android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

This is my code at the point of crash:

gitrep.enqueue(new Callback<MainResponse>() {
        @Override
        public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
            if(response == null){
                Log.d("place","place id null");
            }
            else {
                for (int i =0; i < response.body().getResults().size(); i++){
                    String place_id = response.body().getResults().get(i).getPlaceId();
                    Log.d("place",place_id);

                }

            }
        }

Also this is my MainResponse pojo class

public class MainResponse {
    @SerializedName("html_attributions")
    @Expose
    private List<Object> html_attributions = new ArrayList<Object>();

@SerializedName("next_page_token")
@Expose
private String next_page_token;

@SerializedName("results")
@Expose
private List<Result> results = new ArrayList<Result>();

@SerializedName("status")
@Expose
private String status;

public List<Object> getHtml_attributions() {
    return html_attributions;
}

public void setHtml_attributions(List<Object> html_attributions) {
    this.html_attributions = html_attributions;
}

public String getNext_page_token() {
    return next_page_token;
}

public void setNext_page_token(String next_page_token) {
    this.next_page_token = next_page_token;
}

public List<Result> getResults() {
    return results;
}

public void setResults(List<Result> results) {
    this.results = results;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

}

drachomi
  • 17
  • 1
  • 4
  • 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) – Zoe Apr 03 '18 at 13:12
  • Without seeing your MainResponse class or the JSON it decoded, we can't help you – OneCricketeer Apr 03 '18 at 13:14
  • Also, `response.body()` should only be called once. And you should always null check it, as well as the response itself, anyway – OneCricketeer Apr 03 '18 at 13:15
  • Response.body is null. Any idea what can be the cause? – drachomi Apr 03 '18 at 13:41

1 Answers1

1

I found a solution to my problem. Learnt something tricky about retrofit by the way.

I declared this as base URL:

https://maps.googleapis.com/maps/api/place/

Instead of this:

https://maps.googleapis.com/

Retrofit automatically removes the /maps/place/ and leaves me with an incomplete request. I found out by logging the response i got to string. Then i got the wrong URL as response and i made changes and its worked fine now.

So to retrofit, BASE URL IS BASE URL... nothing more

drachomi
  • 17
  • 1
  • 4