How can I go through this JSON and retrieve the Movies list?

2

I'm having trouble traversing this JSON and extracting data from the Array "movies".

Follow JSON: ( link )

The error is this:

  

java.lang.NullPointerException on for (Movies m: catalogoMovies.movies)

Here's a snippet of code:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(MovieService.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    MovieService service = retrofit.create(MovieService.class);
    final Call<CatalogoMovies> requestCatalogo = service.listCatalog();

    requestCatalogo.enqueue(new Callback<CatalogoMovies>() {
        @Override
        public void onResponse(Call<CatalogoMovies> call, Response<CatalogoMovies> response) {
            if (!response.isSuccessful()) {
                Log.e(TAG, "Error: " + response.code());
            } else {
                CatalogoMovies catalogoMovies = response.body();
                for (Movies m : catalogoMovies.movies) {
                    Log.e(TAG, "TITLE: " + m.title);
                }
            }
        }

        @Override
        public void onFailure(Call<CatalogoMovies> call, Throwable t) {
            Log.e(TAG, "Error: " + t.getMessage());
        }
    });

Error Log

FATAL EXCEPTION: main
   Process: com.example.luciano.saiufilme, PID: 12116
   java.lang.NullPointerException
       at com.example.luciano.saiufilme.HomeActivity$1.onResponse(HomeActivity.java:40)
       at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5017)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
       at dalvik.system.NativeStart.main(Native Method)

CatalogoMovies

public class CatalogoMovies {

     public List<Movies> movies;

}
    
asked by anonymous 17.08.2017 / 06:22

1 answer

2

This NullPointerException occurs because within CatalogoMovies there is no List of Movie !

The structure of the objects is different from the JSON that you added to link .

The List is within% of% co:

{ // OBJETO CatalogoMovies
    "status": "ok",
    "status_message": "Query was successful",
    "data": {            // OBEJTO DATA
        "movie_count": 6334,
        "limit": 20,
        "page_number": 1,
        "movies": [{     // AQUI ESTÁ A LISTA
......

To make it easier (and if you are using Android Studio), I suggest using a plugin to generate these classes automatically.

To install: Top menu:

  

File > Settings

In the window that will appear, look in the side menu:

  

Plugins

Search for: Json2Pojo

Install the plugin, and restart Android Studio!

When you do this, copy the data complete, right click on the package that will add the classes, and go to

  

New > Generate POJO from JSON

PasteyourJSONandenteraclassname:

Obs.:IfanerroroccurswithJson,removeitwithoutanyproblems.

Thispluginwillgeneratetheclassesautomatically,avoidingthistypeoferror.

LINK DO PLUGIN

LINK ABOUT INSTALL PLUGIN ANDROID STUDIO

    
17.08.2017 / 20:46