0

when I click RecyclerView, I want to capture the data of the rooms in the database, but I get problems. how to capture this data when I click RecyclerView. I'm getting this error in the ArrayAdapter section.

Error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

    public class ProductsNewAdapter extends RecyclerView.Adapter<ProductsNewAdapter.ProductViewHolder> {
        Context mCtx;
        private Spinner spinner;
        List<String> Rooms = new ArrayList<String>();
     public ProductsNewAdapter(Context mCtx, List<ProductNew> productList) {
            this.mCtx = mCtx;
            this.productList = productList;

        }
     @Override
        public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mCtx).inflate(R.layout.product_laylout_new,
                    parent, false);
            ProductViewHolder productViewHolder = new ProductViewHolder(view);
            return productViewHolder;

        }

        int selectedPosition=-1;


        @Override
        public void onBindViewHolder(final ProductViewHolder holder, final int position) {
            if(selectedPosition==position)
                holder.itemView.setBackgroundColor(Color.parseColor("#25E9F9"));
            else
                holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"));

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    selectedPosition=position;
                    notifyDataSetChanged();

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

    DatabaseReference refRooms = mDatabase.child("0").child("Rooms");

                refRooms.addValueEventListener(new ValueEventListener() {
               @Override
          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()){
          spinner = (Spinner) v.findViewById(R.id.spinnerMain);
          ViewGroup parent = null;
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.product_laylout_new,
                                    parent, false);

                        String data = snapshot.getValue(String.class);
                       Rooms.add(data);
                          //  addListenerOnSpinnerItemSelection();
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });
                }
            });
1
  • Likely the Context you're passing to constructor is null and it NPEs in LayoutInflater.from(). Have a look at the code where you create this adapter. Commented Jul 11, 2019 at 9:05

1 Answer 1

2

Try this,

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_laylout_new,
            parent, false);
Sign up to request clarification or add additional context in comments.

6 Comments

I'm getting this error: "java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference"
@stackengineer you can use context of parent.
please post the initialization of your recyclerview @stackengineer
I don't understand what you mean @ManojPerumarath
@stackengineer Where are you using this class ProductsNewAdapter ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.