- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图从 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
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());
}
});
}
}
List<Cinema> cinemas = response.body().getCinemas();
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;
}
}
public interface MovieGluApi {
@GET("filmsNowShowing/")
Call<Film> getFilmNames();
@GET("cinemasNearby/")
Call <Cinema> getNearbyCinema();
}
{
"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/
进程虚拟机和系统虚拟机有什么区别? 我的猜测是,进程 VM 没有为该操作系统的整个应用程序提供一种操作系统,而是为某些特定应用程序提供环境。 系统虚拟机为操作系统提供了一个安装环境,就像 Virtua
我在成员函数的上下文中理解 virtual,例如 virtual void frob()。但它在类声明的上下文中意味着什么,例如 class Foo : public virtual Bar? 对于给
根据 react-virtualized 文档,“AutoSizer 组件装饰 React 元素并自动管理宽度和高度属性,以便装饰元素填充可用空间”。 建议通常是加上height: 100%;或 fl
根据 this类似 StackOverflow 问题和其他文章,默认情况下 C# 方法是“非虚拟的”,我认为这意味着您不能在派生类中覆盖它们。 如果那是真的,能否请您向我解释一下,在下面的示例中,我如
我有一个基类Media和几个派生类,即DVD、Book等...基类写成: class Media{ private: int id; string title;
我搜索了一些关于虚函数声明的帖子,相信 =0 在 virtual void test()=0; 是固定句法所以 virtual void test()=NULL; virtual void test(
我正在使用 RV 列表加载具有自定义格式的大型文档。它非常有效,但我遇到了以下两个问题: 我目前在 cellmeasurer 中设置了一个列表 based on this计算行的动态高度(宽度是固定的
我一直在努力制作 this react virtualized table example工作 & 开始严重怀疑我的理智。我创建了一个 react 应用程序,我只是想在 App.js 中使用以下内容呈
我在Windows 7 Pro计算机上安装了Windows Virtual PC和Windows XP Mode。运行XP模式会在Virtual PC上自动安装XP。我想创建第二台与第一台相同的虚拟P
我使用 Virtual PC 来创建新的环境来测试我的安装程序。但我一定是做错了什么,因为内部装有 Vista 或 XP 的 VPC 镜像占用了大约 15GB 的磁盘空间(包括安装在其中的 VS200
是否可以为 Ubuntu 虚拟机动态分配处理器和内存?例如。进程在主机系统上运行,导致处理器的使用率从 30%-70% 上下波动,这些进程还占用 8GB 内存中 3GB-7GB 之间的波动量,即 1G
我正在使用“react-virtualized”来创建一个表。在该表中,一些数据可能显示为 'Brian Vaughn1'。 .此表格单元格应具有 font-weight: bold并且只应呈现文本,
我正在使用“react-virtualized”来创建一个表。在该表中,一些数据可能显示为 'Brian Vaughn1'。 .此表格单元格应具有 font-weight: bold并且只应呈现文本,
我一直在努力理解一段这样的代码: class A { // some class definition } class B { public: virtual A *s
基于 http://en.wikipedia.org/wiki/Virtual_inheritance class Animal { ... }; // Two classes virtually i
我看到 C++ 中的某些函数被声明为 virtual const int getNumber(); 但是如果函数声明如下有什么区别呢? const virtual int getNumber(); 这
问题来自C++ faq。 http://www.parashift.com/c++-faq-lite/protected-virtuals.html 使用公共(public)重载虚拟的代码: clas
这个问题在这里已经有了答案: How is "=default" different from "{}" for default constructor and destructor? (3 个答案
virtual final 函数(final 在基类)是否有任何 vtable/virtual 成本? class B{ public: virtual void fFinal() final
我有一个只包含 exe 文件(没有源代码)的 hello 工具。 你好工具结构: bin helloBin.exe helloRoot.exe conanfile.py conanfile.py
我是一名优秀的程序员,十分优秀!