0

this is my api call where the error is occuring

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


 }

this is my logcat while caaling an api,as u can see that i get the response correctly and i at the backend traceball and it is fine with no errors

    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)

this is the result object

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

this is the Person pojo class

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;
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kethan Chauhan
  • 53
  • 1
  • 2
  • 8

3 Answers3

2

What this means is that response.body().getData() is returning null. Since you can't get the size of null, an exception is thrown.

To make the issue more clear, let's break that for loop into multiple lines. (This is also a good idea so your code will be more efficient. You'll only call size() once in total, as opposed to once every time you go through the loop.

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

You will need to perform a safety check to verify the ResultObject contains the information you're looking for. Hit a breakpoint and look at the contents of the response body.

Software2
  • 2,358
  • 1
  • 18
  • 28
0

You must always check response.body() for null before continue working with it, because server could send you error which goes to response.error(). If you sure that response.body()!=null then check your pojo model, there could be mistakes. Especially in name of fields, that always must be same as in plain json response.

Arthur
  • 769
  • 7
  • 17
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();
    }
 });


 }