2
I am having difficulty going through this JSON and extracting the data from the "Movies Array".
Follows JSON: (https://yts.ag/api/v2/list_movies.json)
The mistake is this:
java.lang.Nullpointerexception for (Movies m : catalogoMovies.Movies)
Follows an excerpt from the 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;
}
You have set up internet access permission ? documentation
– NoobSaibot
Yes, I’ve already added this permission on Androidmanifest.
– martins_
edits the question and puts the generated error log.
– NoobSaibot
I edited it. As I said at the beginning of the post, the error is java.lang.Nullpointerexception in the for line (Movies m : catalogo.Movies) {
– martins_
See if you can help guj.com.br
– NoobSaibot
How is your object
CatalogoMovies
?– LMaker
You could put in your question the contents of Catalogomovies?
– viana
Take a look, I’ve already edited.
– martins_