Adilp Adilp - 1 year ago 84
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 = "";

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

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

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.setSubject((String) object.get("subject"));


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

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



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

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(

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

public Object getItem(int position) {
return position;

public long getItemId(int position) {
return position;

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

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(;
holder.referralImage =(ImageView) rootView.findViewById(;
holder.referralMessage= (TextView) rootView.findViewById(;

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

return rootView;

Answer Source

Use handler

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

and in OkHttpClient use this

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download