0

I am attempting to populate a spinner on Android Studio items from a Firebase Database. However, when trying to run the application, I am met with a null pointer exception. Can anyone tell me what I'm missing???

Code:

public void populateSpinner(){
        DatabaseReference db = FirebaseDatabase.getInstance().getReference();
        db.child("Collection").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final List<String> collections = new ArrayList<String>();

                for (DataSnapshot collectionSnapshot: dataSnapshot.getChildren()) {
                    String collectionName = collectionSnapshot.child("name ").getValue(String.class);
                    collections.add(collectionName);
                }

                Spinner areaSpinner = (Spinner) findViewById(R.id.collectionList);
                ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(AddItem.this, android.R.layout.simple_spinner_item, collections);
                areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                areaSpinner.setAdapter(areasAdapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

Link to image of firebase database [1]: https://i.stack.imgur.com/SCfSI.png

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

3

Your child name has space there.

Change

String collectionName = collectionSnapshot.child("name ").getValue(String.class);

To

String collectionName = collectionSnapshot.child("name").getValue(String.class);

Please make sure your child is same name as your model class.

Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46