gpt4 book ai didi

java - 通过改造调用 api 时,尝试在空对象引用错误上调用虚拟方法 'int java.util.ArrayList.size()'

转载 作者:行者123 更新时间:2023-12-02 02:44:30 33 4
gpt4 key购买 nike

这是我的 api 调用,发生错误

public void loadData(){
pd=new ProgressDialog(this);
pd.setMessage("loading");
pd.show();
mApi= new RetrofitHelper<AuthApi>().getApi(AuthApi.class);
mCall=mApi.studentlist(pageNo);
mCall.enqueue(new Callback<ResultObject<ArrayList<Person>>>() {
@Override
public void onResponse(Call<ResultObject<ArrayList<Person>>> call, Response<ResultObject<ArrayList<Person>>> response) {
for(int i=0;i<response.body().getData().size();i++){
mData.add(response.body().getData().get(i));
}
count=response.body().getCount();
mAdapter=new PersonAdapter(Members.this,R.layout.item_person,mData);
list.setAdapter(mAdapter);
pd.hide();
}

@Override
public void onFailure(Call<ResultObject<ArrayList<Person>>> call, Throwable t) {
Toast.makeText(Members.this,"failed",Toast.LENGTH_SHORT).show();
pd.hide();
}
});


}

这是我在调用 api 时的 logcat,你可以看到我正确地得到了响应,并且我在后端跟踪球,一切都很好,没有错误

    07-01 12:23:56.180 12981-13143/com.example.kethan.project D/OkHttp: --> GET http://192.168.0.107:8000/students/?page=1 http/1.1
07-01 12:23:56.180 12981-13143/com.example.kethan.project D/OkHttp: --> END GET
07-01 12:23:56.183 12981-12981/com.example.kethan.project D/ActivityThreadInjector: clearCachedDrawables.
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: <-- 200 OK http://192.168.0.107:8000/students/?page=1 (80ms)
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Date: Sat, 01 Jul 2017 06:53:53 GMT
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Server: WSGIServer/0.2 CPython/3.5.2
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Content-Length: 849
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Vary: Accept, Cookie
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Content-Type: application/json
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: X-Frame-Options: SAMEORIGIN
07-01 12:23:56.262 12981-13143/com.example.kethan.project D/OkHttp: Allow: GET, POST, HEAD, OPTIONS
07-01 12:23:56.263 12981-13143/com.example.kethan.project D/OkHttp: {"count":37,"next":"http://192.168.0.107:8000/students/?page=2","previous":null,"results":[{"id":35,"name":"kethan","mobile":"8142142827","time":"10:5:0","late":"late by: 5mins 0secs","date":"2017/6/16"},{"id":36,"name":"kethan","mobile":"8142142827","time":"23:28:0","late":"late by: 13hrs 28mins 0secs","date":"2017/06/29"},{"id":37,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":38,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":39,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":40,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":41,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":42,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"}]}
07-01 12:23:56.263 12981-13143/com.example.kethan.project D/OkHttp: <-- END HTTP (849-byte body)
07-01 12:23:56.292 12981-12981/com.example.kethan.project D/AndroidRuntime: Shutting down VM


--------- beginning of crash
07-01 12:23:56.292 12981-12981/com.example.kethan.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kethan.project, PID: 12981
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.example.kethan.project.Members$2.onResponse(Members.java:108)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

这是结果对象

public class ResultObject<T> {

private int count;

private T data;

public int getCount() {
return count;
}

public T getData() {
return data;
}

public void setCount(int count) {
this.count = count;
}

public void setData(T data) {
this.data = data;
}
}

这是 Person pojo 类

public class Person {

private int id;

private String time;

private String late;

private String date;

private String name;

private String mobile;

public String getDate() {
return date;
}

public int getId() {
return id;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getLate() {
return late;
}

public void setLate(String late) {
this.late = late;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public void setDate(String date) {
this.date = date;
}

public void setId(int id) {
this.id = id;
}
}

最佳答案

这意味着 response.body().getData() 返回 null。由于无法获取 null 的大小,因此会引发异常。

为了使问题更清楚,让我们将 for 循环分成多行。 (这也是一个好主意,这样您的代码将会更加高效。您总共只调用 size() 一次,而不是每次执行循环时调用一次。

ArrayList<Person> listOfPersons = response.body().getData(); //This will be null, I suspect.
int size = listOfPersons.size(); //Exception will happen on this line, since listOfPersons is null
for(int i = 0; i < size; ++i)
{
mData.add(listOfPersons.get(i));
}

您需要执行安全检查以验证 ResultObject 包含您要查找的信息。命中断点并查看响应正文的内容。

关于java - 通过改造调用 api 时,尝试在空对象引用错误上调用虚拟方法 'int java.util.ArrayList.size()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44858164/

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