androidfirebaseandroid-studioandroid-arrayadaptercustom-arrayadapter

ImageView setOnClickListener on a null object reference in custom adapter


I took this adapter from the internet and I got a problem:

Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
.Consts.CustomFriendsRequets.getView

Here CustomFriendRequest is my adapter, and its copy works fine, but this one doesn't want. The error refers to line 104, namely viewHolder.add.setOnClickListener (v ->. I spent several days going through the code, trying to figure out what was wrong. Here is the complete adapter code. CustomFriendsRequets:

class CustomFriendsRequets extends ArrayAdapter<addFriends> implements View.OnClickListener{
    private ArrayList<addFriends> dataSet;
    private String uid, uid2;
    private DatabaseReference isRef;
    Context mContext;



    // View lookup cache
    private static class ViewHolder {
        TextView txtName;
        TextView txtType;
        ImageView info;
        ImageView add;
        ImageView del;
    }

    public CustomFriendsRequets(ArrayList<addFriends> data, Context context) {
        super(context, R.layout.friends_requests_item, data);
        this.dataSet = data;
        this.mContext=context;

    }

    @Override
    public void onClick(View v) {

        int position=(Integer) v.getTag();
        Object object= getItem(position);
        addFriends dataModel=(addFriends) object;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        addFriends dataModel = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.friends_item, parent, false);
            viewHolder.txtName = convertView.findViewById(R.id.tvDescr);
            viewHolder.txtType = convertView.findViewById(R.id.tvPrice);
            viewHolder.info = convertView.findViewById(R.id.uImgreqs);
            viewHolder.add = convertView.findViewById(R.id.ibAdd);
            viewHolder.del = convertView.findViewById(R.id.ibDel);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();if(user!=null){uid=user.getUid();}
        uid2 = dataModel.uid2;
        viewHolder.txtName.setText(dataModel.username1);
        viewHolder.txtType.setText("хочет добавить вас в друзья");
        viewHolder.info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), EnemyProfile.class);
                intent.putExtra(Constant.UID, dataModel.uid2);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                v.getContext().startActivity(intent);
            }
        });
        isRef = FirebaseDatabase.getInstance().getReference();
        viewHolder.add.setOnClickListener(v ->  //error here
                isRef.child("Users").child("info").child(uid2).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                Users info = snapshot.getValue(Users.class);
                if (info!=null){
                    String name = info.username;
                    String id = info.uid;
                    addFriends adf = new addFriends(id, name);
                    isRef.child("Users").child(uid).child("Friends").setValue(adf).
                            addOnSuccessListener(aVoid ->
                                    isRef.child("Users").child("info").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot snapshot1) {
                                    Users minfo = snapshot1.getValue(Users.class);
                                    if (minfo!=null){
                                        String mname = minfo.username;
                                        String mid = minfo.uid;
                                        addFriends madf = new addFriends(mid, mname);
                                        isRef.child("Users").child(uid2).child("Friends").setValue(madf)
                                                .addOnSuccessListener(aVoid1 -> {
                                                    isRef.child("Users").child(uid).child("Friends-request").child(uid2).removeValue();
                                                    isRef.child("Users").child(uid2).child("Friends-request").child(uid).removeValue();
                                                });
                                    }
                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError error) {
                                    Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            }));
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }));
        viewHolder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isRef.child("Users").child(uid).child("Friends-request").child(uid2).removeValue();
                isRef.child("Users").child(uid2).child("Friends-request").child(uid).removeValue();
            }
        });
        // Return the completed view to render on screen
        return convertView;
    }
}

and here I call it in the activity on the listview:

private void getReqs() {
        DatabaseReference isRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Friends-request");
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (reqsTemp.size() > 0){reqsTemp.clear();}
                for (DataSnapshot ds : snapshot.getChildren()){
                    addFriends reqs = ds.getValue(addFriends.class);
                    if (reqs!=null) {
                        reqsTemp.add(reqs);
                    }
                }
                reqsadapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(Friends.this, error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        };
        isRef.addValueEventListener(valueEventListener);
    }

by the way here is the element code from the adapter:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="96dp"
    android:orientation="horizontal">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewsdd"
        android:layout_width="86dp"
        android:layout_height="86dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        app:cardBackgroundColor="@color/common_google_signin_btn_text_dark_disabled"
        app:cardCornerRadius="50dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/uImgreqs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:src="@mipmap/ic_launcher_round"
            android:contentDescription="@string/todo" />
    </androidx.cardview.widget.CardView>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvDescr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text=""
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tvPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/ibAdd"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@android:drawable/ic_input_add" />

        <ImageView
            android:id="@+id/ibDel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_dialog_close_light" />
    </LinearLayout>
</LinearLayout>

the add button should record the id of both users to each other, and del accordingly delete the request. uImgreqs is just a photo of the user, and both textviews are user data of the username, etc. If anything needs to be added, I will be glad to add, because I am in despair, and the deadlines are running out


Solution

  • God, boooooo well noooo. Android Studio, I adore you. I found a solution, it turned out that the problem was due to (v ->, I or the studio translated the OnClick function into a lambda and because of this it returned a null object ... 3 and a half days ... I spent more than three days on this, omg

    viewHolder.add.setOnClickListener(new View.OnClickListener() {
                                                  @Override
                                                  public void onClick(View v) {});
    

    No, it`s not win

    uh, no, it didn't solve the problem completely and now throws an error on every imageView .setOnClicklistener ...

    I found the problem, I misdated the label:

    convertView = inflater.inflate(R.layout.friends_item, parent, false);
    

    very unremarkable line, facepalm