jaguar
jaguar

Reputation: 109

Facing problems with the retrofit api. Error: java.lang.NullPointerException: Attempt to invoke interface method on a null object reference

I am new to the Retrofit API and I am struggling with it. I followed the tutorial on Vogella and was able to get data from StackExchange and display it in a list view. But I am getting the error below when I try to get data from TMDB.

java.lang.NullPointerException: Attempt to invoke interface method
    'java.lang.Object[] java.util.Collection.toArray()' on a null object reference,

Here is the code to display the "title" from the StackExchange url in a list view (this is working fine).

MainActivity

public class MainActivity extends ListActivity implements Callback<StackOverflowQuestion> {

    ArrayAdapter<Question> madapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        madapter=new ArrayAdapter<Question>(this,android.R.layout.simple_list_item_1,android.R.id.text1,new ArrayList<Question>());
        setListAdapter(madapter);
        Retrofit retrofit= new Retrofit.Builder()
            .baseUrl("http://api.stackexchange.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
        StackOvreflowAPI stackOverflowAPI = retrofit.create(StackOvreflowAPI.class);

        Call<StackOverflowQuestion> call = stackOverflowAPI.load();
        call.enqueue(this);
    }

    @Override
    public void onResponse(Response<StackOverflowQuestion> response, Retrofit retrofit) {

        ArrayAdapter<Question> adapter = (ArrayAdapter<Question>) getListAdapter();
        adapter.clear();
        adapter.addAll(response.body().items);
    }

    @Override
    public void onFailure(Throwable t) {

    }
}

Model class: Question

public class Question {
    public String name;

    @Override
    public String toString() {
        return (name);
    }
}

StackOverflowQuestion

public class StackOverflowQuestion {
    List<Question> items;
}

StackOverflowAPI

public interface StackOvreflowAPI {
    @GET("/2.2/questions?order=desc&sort=activity&tagged=android&site=stackoverflow")
    Call<StackOverflowQuestion> load();
}

But when I use the same code to get overview from the TMDB url I get the error:

java.lang.NullPointerException: Attempt to invoke interface method    
    'java.lang.Object[] java.util.Collection.toArray()' on a null object reference.

I only changed the URL, the interface and the model class.

Changes made in the MainActivity:

Retrofit retrofit= new Retrofit.Builder()
            .baseUrl("https://api.themoviedb.org")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

Using .baseUrl("https://api.themoviedb.org") instead of .baseUrl("https://api.stackexchange.com").

Changes made in the Interface:

public interface StackOvreflowAPI {
    @GET("/3/movie/popular?api_key=ab5aee0b18da89dd9e026d35754c24f1")
    Call<StackOverflowQuestion> load();
}

Using @GET("/3/movie/popular?api_key=ab5aee0b18da89dd9e026d35754c24f1") instead of @GET("/2.2/questions?order=desc&sort=activity&tagged=android&site=stackoverflow")

Changes made in the Question class:

public class Question {
    public String overview;

    @Override
    public String toString() {
        return (overview);
    }
}

Changed title to overview.

But on running this the app crashes with the error:

FATAL EXCEPTION: main
    Process: com.example.maver_000.practise_retrofit, PID: 4270
    java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
    at java.util.ArrayList.addAll(ArrayList.java:188)
    at android.widget.ArrayAdapter.addAll(ArrayAdapter.java:195)
    at com.example.maver_000.practise_retrofit.MainActivity.onResponse(MainActivity.java:40)
    at retrofit.ExecutorCallAdapterFactory$ExecutorCallback$1.run(ExecutorCallAdapterFactory.java:86)
    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:5294)
    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:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

Upvotes: 0

Views: 8255

Answers (1)

Abhishek Sinha
Abhishek Sinha

Reputation: 435

Check the response structure being returned by the TMDB API, I think 'overview' may not be a direct root object, thus Gson conversion of response may be failing.

Looking at the API response, Change your StackOverFlowQuestion class' variable name to results or add a @SerializedName to match the json name, then it should work

Upvotes: 2

Related Questions