Я пытаюсь вернуть список кинотеатров из MovieGlu API, но получаю исключение с нулевым указателем в строке 98 моего кода. Фильмы возвращаются, и я пытаюсь заставить кинотеатры вернуться таким же образом. API действительно возвращает их в почтальоне. Я использую Retrofit2 для возврата данных.
Ошибка при запуске приложения:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.retrofitapp.model.nearbycinemas.Cinema.getCinemas()' on a null object reference Retrofit
MainActivity.class
public class MainActivity extends AppCompatActivity {
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewResult = findViewById(R.id.text_view_result);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader("api-version", "v200")
.addHeader("Authorization", "Basic *******************")
.addHeader("client", "WALS_1")
.addHeader("x-api-key", "**********************************")
.addHeader("device-datetime", "2018-09-14T08:30:17.360Z")
.addHeader("territory", "UK")
.addHeader("Geolocation", "****************;*************")
.build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://api-gate2.movieglu.com/")
.client(client.build())
.build();
MovieGluApi movieGluApi = retrofit.create(MovieGluApi.class);
Call<Film> filmCall = movieGluApi.getFilmNames();
filmCall.enqueue(new Callback<Film>() {
@Override
public void onResponse(Call<Film> call,Response<Film> response) {
if (!response.isSuccessful()){
textViewResult.setText("Code: " + response.code());
Log.d("dfkjf", response.toString());
return;
}
assert response.body() != null;
List<Film> films = response.body().getFilms();
Log.d("dfkjf", response.toString());
for (Film film : films){
String content = "";
content += "Film ID: " + film.getFilmId() +"\n";
content += "IMDB ID: " + film.getImdbId() +"\n";
content += "Film Name: " + film.getFilmName() +"\n\n";
Log.d("Film name", film.toString());
textViewResult.append(content);
}
}
@Override
public void onFailure(Call<Film> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
Call<Cinema> cinemaCall = movieGluApi.getNearbyCinema();
cinemaCall.enqueue(new Callback<Cinema>() {
@Override
public void onResponse(Call<Cinema> call, Response<Cinema> response) {
if (!response.isSuccessful()){
textViewResult.setText("Code: " + response.code());
Log.d("Response", "Message: " + response.toString());
return;
}
assert response.body() != null;
List<Cinema> cinemas = response.body().getCinemas();
Log.d("Response", "response: " + response.toString());
for (Cinema cinema : cinemas){
String cinemaContent = "";
cinemaContent += "Cinema Name: " + cinema.getCinemaName() +"\n";
cinemaContent += "Cinema Address 1 " + cinema.getAddress() +"\n";
cinemaContent += "Cinema Address: 2 " + cinema.getAddress2() +"\n\n";
cinemaContent += "Cinema City: " + cinema.getCity() +"\n\n";
cinemaContent += "Cinema Country " + cinema.getCounty() +"\n\n";
cinemaContent += "Cinema PostCode: " + cinema.getPostcode() +"\n\n";
textViewResult.append(cinemaContent);
}
}
@Override
public void onFailure(Call<Cinema> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
}
Line 98 Error
List<Cinema> cinemas = response.body().getCinemas();
кинотеатры. java (Модель)
public class Cinema {
@SerializedName("cinema_id")
@Expose
private Integer cinemaId;
@SerializedName("cinema_name")
@Expose
private String cinemaName;
@SerializedName("address")
@Expose
private String address;
@SerializedName("address2")
@Expose
private String address2;
@SerializedName("city")
@Expose
private String city;
@SerializedName("county")
@Expose
private String county;
@SerializedName("postcode")
@Expose
private String postcode;
@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lng")
@Expose
private Double lng;
@SerializedName("distance")
@Expose
private Double distance;
@SerializedName("logo_url")
@Expose
private String logoUrl;
List<Cinema> cinemas;
public List<Cinema> getCinemas() {
return cinemas;
}
public Integer getCinemaId() {
return cinemaId;
}
public void setCinemaId(Integer cinemaId) {
this.cinemaId = cinemaId;
}
public String getCinemaName() {
return cinemaName;
}
public void setCinemaName(String cinemaName) {
this.cinemaName = cinemaName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
public Double getDistance() {
return distance;
}
public void setDistance(Double distance) {
this.distance = distance;
}
public String getLogoUrl() {
return logoUrl;
}
public void setLogoUrl(String logoUrl) {
this.logoUrl = logoUrl;
}
}
MovieGluApi (Интерфейс)
public interface MovieGluApi {
@GET("filmsNowShowing/")
Call<Film> getFilmNames();
@GET("cinemasNearby/")
Call <Cinema> getNearbyCinema();
}
Возвращаемые данные JSON
{
"cinemas": [
{
"cinema_id": 9461,
"cinema_name": "Broadway Cinema - Nottingham",
"address": "14-18 Broad Street",
"address2": "",
"city": "Nottingham",
"county": "Nottinghamshire",
"postcode": "NG1 3AL",
"lat": 52.954319,
"lng": -1.14397,
"distance": 45.481624870276,
"logo_url": "https://assets.movieglu.com/chain_logos/uk/UK-0-sq.jpg"
},
{
"cinema_id": 41088,
"cinema_name": "Regent Street Cinema",
"address": "307 Regent Street",
"address2": "",
"city": "London",
"county": "Greater London",
"postcode": "W1B 2HW",
"lat": 51.51683,
"lng": -0.14277,
"distance": 100.14079068585,
"logo_url": "https://assets.movieglu.com/chain_logos/uk/UK-0-sq.jpg"
},
{
"cinema_id": 9438,
"cinema_name": "Scott Cinemas - The Atrium Cinema",
"address": "Atrium Building",
"address2": "King Street",
"city": "East Grinstead",
"county": "West Sussex",
"postcode": "RH19 3DJ",
"lat": 51.1264,
"lng": -0.00885,
"distance": 123.65933949165,
"logo_url": "https://assets.movieglu.com/chain_logos/uk/UK-1198-sq.jpg"
},
{
"cinema_id": 9000,
"cinema_name": "Scott Cinemas - Radway Cinema",
"address": "Radway Place",
"address2": "",
"city": "Sidmouth",
"county": "Devon",
"postcode": "EX10 8TL",
"lat": 50.682831,
"lng": -3.23856,
"distance": 136.92054971699,
"logo_url": "https://assets.movieglu.com/chain_logos/uk/UK-1198-sq.jpg"
},
{
"cinema_id": 10355,
"cinema_name": "New Carlton Cinema",
"address": "Market Street",
"address2": "Market Hall",
"city": "Okehampton",
"county": "Devon",
"postcode": "EX20 1HN",
"lat": 50.739399,
"lng": -4.00368,
"distance": 150.44700252827,
"logo_url": "https://assets.movieglu.com/chain_logos/uk/UK-1262-sq.jpg"
}
],
"status": {
"count": 5,
"state": "OK",
"method": "cinemasNearby",
"message": null,
"request_method": "GET",
"version": "****",
"territory": "UK",
"device_datetime_sent": "2020-05-17T08:30:17.360Z",
"device_datetime_used": "2020-05-17 08:30:17"
}
}