gpt4 book ai didi

java.lang.NullPointerException : Attempt to invoke virtual method on a null object reference Retrofit

转载 作者:行者123 更新时间:2023-12-01 16:33:42 25 4
gpt4 key购买 nike

我试图从 MovieGlu API 返回电影院列表,但在代码的第 98 行出现空指针异常。电影回归,我正在努力让电影院以同样的方式回归。 API 确实在 postman 中返回它们。我正在使用 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());
}
});
}
}

第 98 行错误

List<Cinema> cinemas = response.body().getCinemas();

cinemas.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(接口(interface))

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"
}
}

最佳答案

因为你忘记了:
@SerializedName("电影院")
@暴露
列出电影院;

希望对你有帮助。

关于java.lang.NullPointerException : Attempt to invoke virtual method on a null object reference Retrofit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62004500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com