Adilp Adilp - 3 months ago 11
Android Question

Custom listview adapter size null

Currently I am trying to populate with my custom listview with a JSON

It works fine with dummy data, and my json request is fine.

All this code is in onCreate view, I know we are supposed to use Async task or some sort to spawn a new thread to pull data, but I wanted to just put it here for testing purposes so I know I can pull data first.

OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new BasicAuthInterceptor()).addNetworkInterceptor(new StethoInterceptor()).build();

String url = "http://10.0.2.2:3000/response";

Request request = new Request.Builder().url(url)
.build();


client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i(TAG, "call api error");
Log.e("Volley", e.toString());
}

@Override
public void onResponse(Call call, okhttp3.Response response) {

try {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
final String body = response.body().string();

JSONObject Jobject = new JSONObject(body);


JSONArray Jarray = Jobject.getJSONArray("inboxMsgList");

Log.e("Array", String.valueOf(Jarray.length()));
//Initializing my ArrayLis here I already defined it where I have my other variables defined.
arrMessageData = new ArrayList<>();

JSONObject object = Jarray.getJSONObject(0);

MessageData msg3 = new MessageData();
msg3.setName((String) object.get("fromUser"));
msg3.setMessage((String) object.get("message"));
msg3.setImage(R.drawable.msgone);
msg3.setRead(true);
msg3.setSubject((String) object.get("subject"));

arrMessageData.add(msg3);

Log.e("DetailObject", msg3.getName());


// Log.e("FullJsonReply", body.toString());
} catch (Exception e){
Log.e("Volley2", e.toString());
}

}

});


adapter = new CustomAdapterReferral(getActivity().getBaseContext(), arrMessageData);
usersListView.setAdapter(adapter);


The error I get is:


java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.adilpatel.vitalengine.ListAdapters.CustomAdapterReferral.getCount(CustomAdapterReferral.java:40)


Line 40 is my getCount in my Adapter

public int getCount() {
return arrMessageData.size();
}


I put a break point right where it returns the size

enter image description here

Looks like the size is 1 but it still gives null pointer exception. Is it because adapter is null? What is adapter?

Edit: Here is my CustomAdapterReferral:

public class CustomAdapterReferral extends BaseAdapter {

ArrayList<MessageData> arrMessageData;
Context context;
private static LayoutInflater inflater = null;

public CustomAdapterReferral(Context mainActivity, ArrayList<MessageData> arrMessageData){


this.arrMessageData = arrMessageData;

context = mainActivity;


inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}



public int getCount() {
return arrMessageData.size();
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

public class ViewHolder {
TextView referralName;
ImageView referralImage;
TextView referralMessage;


}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder=new ViewHolder();

View rootView = inflater.inflate(R.layout.list_item_referral, null);


holder.referralName = (TextView) rootView.findViewById(R.id.referralName);
holder.referralImage =(ImageView) rootView.findViewById(R.id.referralImage);
holder.referralMessage= (TextView) rootView.findViewById(R.id.referralMessage);

holder.referralMessage.setText("" + arrMessageData.get(position).getMessage());
holder.referralName.setText("" + arrMessageData.get(position).getName());
holder.referralImage.setImageResource(arrMessageData.get(position).getImage());

return rootView;
}
}

ARH ARH
Answer

Use handler

 private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    adapter = new CustomAdapterReferral(getActivity().getBaseContext(), arrMessageData);
                    usersListView.setAdapter(adapter);
                    break;
                default:
                    Log.d("TAG", msg.what + " ? ");
                    break;
            }
        }
    };

and in OkHttpClient use this

 @Override
        public void onResponse(Call call, okhttp3.Response response)  {
  //YOUR CODE
 //END
handler.sendEmptyMessage(1);

}
Comments