2017-07-01 33 views
-2

的API,這是我的API調用其中誤差是發生嘗試上的空對象引用錯誤調用虛擬方法「詮釋java.util.ArrayList.size()」,而主叫通過改型

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(); 
     } 
    }); 


} 

此是我logcat的同時caaling的API,如u可以看到,我得到正確的響應,我在後端traceball,它是好的,沒有錯誤

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

這就是人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; 
} 
} 
+0

請在這裏添加您的JSON響應 –

+0

因爲你的JSON性反應是空..你不檢查空指針的性反應..所以jsonArray大小爲null –

+0

@ManthanPatel我已經在logcat中添加JSON,盡見 –

回答

0

在繼續使用它之前,您必須始終檢查response.body()爲null,因爲如果你確定response.body()!= null,那麼檢查你的pojo模型,可能會有錯誤,尤其是在f該字段總是必須與普通的json響應相同。

+0

是的,我認爲我的pojo類錯誤 –

2

這意味着是response.body().getData()被返回null。由於您無法獲取空值的大小,因此會引發異常。

爲了讓問題更清楚,讓我們將該循環分成多行。 (這也是一個好主意,所以你的代碼效率會更高。不過只會在總打電話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包含您正在查找的信息打一個斷點並查看響應正文的內容

+0

我已更新logcat(請參見),因爲我在json中獲得響應,所以響應不爲空,我猜 –

+0

更新我的答案,更詳細。希望這能指出你的方向。 :) – Software2

0
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) { 
// check responce null or not  
    if(response!=null && response.size()>0){ 
     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(); 
    } 
}); 


} 
相關問題