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;
}