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