I'm trying to use an API that returns JSON, and I'm using Retrofit to get the response and parse it
the API retruns JSON like the following :
{
"total": 1244881,
"totalHits": 500,
"hits": [
{
"id": 5205518,
"pageURL": "https://pixabay.com/photos/landscape-sea-sky-clouds-sunset-5205518/",
"type": "photo",
"tags": "landscape, sea, sky",
"previewURL": "https://cdn.pixabay.com/photo/2020/05/22/14/04/landscape-5205518_150.jpg",
"webformatWidth": 640,
"webformatHeight": 427,
...
...
},
...
...
]
}
And for The GSON to convert this json into an Object I created two classes :
the first is : RquestModel.java
public class RequestModel {
@SerializedName("total")
private long total;
@SerializedName("totalHits")
private long totalHits;
@SerializedName("hits")
private List<Image> hits;
//Getters and setters down here...
}
and the second class for the array in the json is :
public class Image {
@SerializedName("id")
private long id;
@SerializedName("pageURL")
private String pageURL;
@SerializedName("type")
private String type;
@SerializedName("tags")
private String tags;
@SerializedName("previewURL")
private String previewURL;
@SerializedName("previewWidth")
private long previewWidth;
@SerializedName("previewHeight")
private long previewHeight;
@SerializedName("webformatURL")
private String webformatURL;
@SerializedName("webformatWidth")
private long webformatWidth;
@SerializedName("webformatHeight")
private long webformatHeight;
@SerializedName("largeImageURL")
private String largeImageURL;
@SerializedName("imageWidth")
private long imageWidth;
@SerializedName("imageHeight")
private long imageHeight;
@SerializedName("imageSize")
private long imageSize;
@SerializedName("views")
private long views;
@SerializedName("downloads")
private long downloads;
@SerializedName("favorites")
private long favorites;
@SerializedName("likes")
private long likes;
@SerializedName("comments")
private long comments;
@SerializedName("user_id")
private long userId;
@SerializedName("user")
private String user;
@SerializedName("userImageURL")
private String userImageURL;
//Getters and Setters down here .....
}
And for the interface that the GSON converter uses is like this :
public interface ImageAPIService {
@GET(".")
public Call<RequestModel> getRequest();
}
and When I try to use the response from the Callback like this :
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ImageAPIService api = retrofit.create(ImageAPIService.class);
Call<RequestModel> request = api.getRequest();
request.enqueue(new Callback<RequestModel>() {
@Override
public void onResponse(Call<RequestModel> call, Response<RequestModel> response) {
mViewModel.setTxt(response.body().getTotal() + "");
}
@Override
public void onFailure(Call<RequestModel> call, Throwable t) {
}
});
that gives an Exception of type: java.lang.NullPointerException
:
java.lang.NullPointerException: Attempt to invoke virtual method 'long com.mapps.pixabayclient.models.RequestModel.getTotal()' on a null object reference
I tried to debug it and everything seems fine to me, I don't know what I have missed. if someone could notice the mistake, I'd be very thankful.
And thanks for your help in advance.