Я пытаюсь получить ответ от json, но получаю ошибку нулевого указателя
Вот мой модифицированный интерфейс
public interface RequestGeo {
@Headers("user-key: mykey")
@GET("geocode?lat=49.195061&lon=16.606836")
Call<JSONResponse> getJSON();
}
На MainActivity я получил этот код
private void loadJSON() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://developers.zomato.com/api/v2.1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestGeo request = retrofit.create(RequestGeo.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
geoCodeData = new ArrayList<>(Arrays.asList(jsonResponse.getGeocode()));
adapter = new RestaurantListAdapter(geoCodeData);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error", t.getMessage());
}
});
}
Ошибка nullpointerexception здесь
geoCodeData = new ArrayList<>(Arrays.asList(jsonResponse.getGeocode()));
Вот мой файл jsonResponse.java
public class JSONResponse {
private Geocode[] geocode;
public Geocode[] getGeocode(){
return geocode;
}
}
UPDATE
Класс Geocode.java, в котором я записываю переменные точно в виде объектов json, которые я хочу получить (их больше, но я их не хочу).
@Entity
public class Geocode {
@NonNull
@PrimaryKey private String title;
private String latitude;
private String longitude;
private String city_name;
public Geocode(String title, String latitude, String longitude, String city_name) {
this.title = title;
this.latitude = latitude;
this.longitude = longitude;
this.city_name = city_name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getCity_name() {
return city_name;
}
public void setCity_name(String city_name) {
this.city_name = city_name;
}
}