1
I need to take the data that comes inside a Sponse, but when I get a get on the Sponse object it gives error saying it is null. I think it has something to do with my get being done before Sponse arrives, but I don’t know how to fix it. Below are the codes:
The Onresponse:
@Override
public void onResponse(Call<ResponseDTO> call, Response<ResponseDTO> response) {
if(response.isSuccessful()) {
Log.i("conexao", "succesful");
System.out.println("conectou");
responseDTO = response.body();
System.out.println(response.code());
for(int i = 0; i < 5; i++) {
Log.i("hq", responseDTO.getData().getComics().get(i).getTitle());
}
} else {
Log.i("conexao", "not succesful");
System.out.println("conectou mas deu errado");
//responseDTO = null;
}
}
Response object (what I want to pick up is this date attribute, but accuses that it is null)
public class ResponseDTO {
private String code;
private String status;
private MarvelData data;
public ResponseDTO(String code, String status, MarvelData data) {
this.code = code;
this.status = status;
this.data = data;
}
//Gets e sets...
}
Where I’m calling the methods:
public class ComicFacade {
Controller comicController;
ResponseDTO responseDTO;
MarvelData marvelData;
public ComicFacade() {
this.comicController = new Controller();
this.comicController.create();
this.responseDTO = comicController.getResponseDTO();
}
public List<Comic> getComics() {
marvelData = responseDTO.getData();
return marvelData.getComics();
}
}
Error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ecommercemarvel/com.example.ecommercemarvel.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.ecommercemarvel.model.MarvelData com.example.ecommercemarvel.model.ResponseDTO.getData()' on a null object reference
How do I get the data only when the request is complete?