空对象引用错误 'void android.widget.TextView.setText(java.lang.CharSequence)'

Error 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

我有这个错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

在此 - holder.Plan.setText(model.getTypeOfPlan());

和这个 -public class padapter extends FirestoreRecyclerAdapter {

怎么解决,希望大家帮忙

这是代码

    public class padapter extends FirestoreRecyclerAdapter<addp, padapter.profileHolder> {


    private  OnItemClickListener listener;
    public padapter(@NonNull FirestoreRecyclerOptions<addp> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull profileHolder holder, int position, @NonNull addp model) {
        holder.textViewName.setText(model.getName());
        holder.Plan.setText(model.getTypeOfPlan());
        holder.Price.setText(model.getPrice());
    }


    @NonNull
    @Override
    public profileHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.profile_item, parent, false);
        View Plan = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_cart, parent, false);
        View Price = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_cart, parent, false);


        return new profileHolder(v);

    }

    class profileHolder extends RecyclerView.ViewHolder {
        TextView textViewName;
        TextView Plan;
        TextView Price;




        public profileHolder(@NonNull View itemView) {
            super(itemView);

            textViewName = itemView.findViewById(R.id.Text_view_name);
            Plan = itemView.findViewById(R.id.View_Plan_cart);
            Price = itemView.findViewById(R.id.View_Plan_price);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    if(position != RecyclerView.NO_POSITION && listener != null){
                        listener.onItemClick(getSnapshots().getSnapshot(position),position);
                    }
                }
            });

        }
    }

 public interface  OnItemClickListener{
        void onItemClick(DocumentSnapshot documentSnapshot, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;

    }

}

我怀疑 model.getTypeOfPlan() 正在返回 null,在这种情况下,您需要提供更多代码以显示它的来源。

我会给你一个笼统的答案,因为这是你在开发 Android/Java 代码时会多次遇到的问题。 (java.lang.NullPointerException).

你就是这样解决的;

  1. 确保检查所有可空项以正确初始化。 例如在你的情况下,而不是第二次猜测哪个对象是空的包括 if 语句来检查空值,就像这样,

    if(holder != null ){
    
       if(model != null){
          holder.textViewName.setText(model.getName());
          holder.Plan.setText(model.getTypeOfPlan());
          holder.Price.setText(model.getPrice());
       }else
          Log.e(getClass().getSimpleName(),"model object is Null...");
    }else 
      Log.e(getClass().getSimpleName(), "holder object is null...");
    

您可以在调试期间使用它,一旦您建立了导致 NullPointerException 的对象,您就可以继续正确初始化它,然后您可以摆脱 if 语句或检查。

希望这对现在和将来有所帮助...:)