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

huangapple go评论71阅读模式
英文:

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

问题

适配器:

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

活动:

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

运行此代码会导致以下错误:

java.lang.NullPointerException: 尝试在空对象引用上调用接口方法 'int java.util.List.size()'
at com.dell.mycampus.view.adapter.DellTransportAdapter.getItemCount

customers 是否被初始化?

英文:

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

private List&lt;DellTransportList&gt; customers;

    public DellTransportAdapter(List&lt;DellTransportList&gt; 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&lt;List&lt;DellTransportList&gt;&gt; call = mApiInterface.getcustomerName();
    call.enqueue(new Callback&lt;List&lt;DellTransportList&gt;&gt;() {
        @Override
        public void onResponse(@NotNull Call&lt;List&lt;DellTransportList&gt;&gt; call, @NotNull Response&lt;List&lt;DellTransportList&gt;&gt; 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&lt;List&lt;DellTransportList&gt;&gt; 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?

答案1

得分: 1

你应该使用addAll来替代=

> addAll(Collection<? extends E> c):将指定集合中的所有元素追加到此列表的末尾,顺序与指定集合的迭代器返回的顺序相同。如果在操作进行时修改了指定的集合,则此操作的行为是未定义的。

<s>不要</s>

mList = response.body();

而应该

mList.addAll(response.body());
英文:

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.

<s>Don't</s>

mList = response.body();

Do

 mList.addAll(response.body());

答案2

得分: 0

private List<DellTransportList> customers;未初始化。请检查您的响应并初始化ArrayList以避免此问题。

英文:

private List&lt;DellTransportList&gt; customers; is not initialized. Please check your response and you ArrayList to avoid this issue.

答案3

得分: 0

同意@Ashish的回答,同时您还需要使用setLayoutManager()为您的可循环视图设置布局管理器

mList = response.body();
mAdapter = new DellTransportAdapter(mList);
mRecycler.setLayoutManager(new LinearLayoutManager(this));
mRecycler.setAdapter(mAdapter);
英文:

Agree with @Ashish's answer also you have to set LayoutManager for your recycler using setLayoutManager()

mList = response.body();
mAdapter = new DellTransportAdapter(mList);
mRecycler.setLayoutManager(new LinearLayoutManager(this));
mRecycler.setAdapter(mAdapter);

huangapple
  • 本文由 发表于 2020年9月30日 13:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64131464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定