在onBindViewHolder方法上,我得到一个错误,我在一个空引用对象上调用.get(position)。
不知道如何解决此问题。
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int
position) {
String desc_data = event_list.get(position).getDesc();
holder.setDescText(desc_data);
}
这是我在LogCat中遇到的错误。 `
2019-09-09 19:16:30.479 14504-14504/com.example.iacs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.iacs, PID: 14504
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.example.iacs.EventRecyclerAdapter.onBindViewHolder(EventRecyclerAdapter.java:38)
at com.example.iacs.EventRecyclerAdapter.onBindViewHolder(EventRecyclerAdapter.java:13)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
`
答案 0 :(得分:0)
写这张支票
if(event_list != null && event_list.size >= position ){
String desc_data = event_list.get(position).getDesc();
holder.setDescText(desc_data+"");
}
我希望此检查能够解决您的空值异常