Skip to content
Advertisement

Attempt to invoke interface method ‘int java.util.List.size()’ on a null object reference – while calling retrofit

I wrote an adapter and activity code to fetch the data. Code is given below.
Adapter:

private List<DellTransportList> customers;

    public DellTransportAdapter(List<DellTransportList> list) {
        this.customers = list;
    }

    @NonNull
    @Override
    public DellTransportAdapter.MyDriveViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.drive_recycler_item,parent,false);
        return new MyDriveViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyDriveViewHolder holder, int position) {
        DellTransportList customer = customers.get(holder.getAdapterPosition());

        TextView dataText = holder.mEmployeeId;
        TextView dataName = holder.mName;

        dataText.setText(customer.getEmployeeid());
        dataName.setText(customer.getName());

    }

    @Override
    public int getItemCount() {
        return customers.size();
    }

    public class MyDriveViewHolder extends RecyclerView.ViewHolder {

        public TextView mName, mEmployeeId;

        public MyDriveViewHolder(View view) {
            super(view);
            mName = view.findViewById(R.id.mtv_view);
            mEmployeeId = view.findViewById(R.id.mtv_data_view);
        }
    }  

Activity:

// RecyclerView
    mRecycler = findViewById(R.id.rv_drive);
    mRecycler.setHasFixedSize(true);
    mApiInterface = RetrofitInstance.getRetrofitInstance().create(DellTransportApiInterface.class);
    mList = new ArrayList();
    Call<List<DellTransportList>> call = mApiInterface.getcustomerName();
    call.enqueue(new Callback<List<DellTransportList>>() {
        @Override
        public void onResponse(@NotNull Call<List<DellTransportList>> call, @NotNull Response<List<DellTransportList>> response) {
            mList = response.body();
            mAdapter = new DellTransportAdapter(mList);
            mRecycler.setAdapter(mAdapter);
            mRecycler.setLayoutManager(new LinearLayoutManager(DriveActivity.this));
            mAdapter.notifyDataSetChanged();
            // Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(@NotNull Call<List<DellTransportList>> call, @NotNull Throwable t) {
        }
    });
} 

Running that code gives me this error:

java.lang.NullPointerException: Attempt to invoke interface method ‘int java.util.List.size()’ on a null object reference at com.dell.mycampus.view.adapter.DellTransportAdapter.getItemCount

Is customers initialized?

Advertisement

Answer

You should use addAll instead of =

addAll(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Don’t

mList = response.body();

Do

 mList.addAll(response.body());
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement