-1

I have a node in firebase database that I want to retrieve and put it in a spinner I made array list then looped through data snapshot but still give nullPointerException I stored the value of the child in firebase using push method

I tried everything but it didn't work

Firebase Node

Model class

public class Purpose {

private String name;

public Purpose(){

}

public Purpose(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
 }

Add data class

        puposedRef = FirebaseDatabase.getInstance().getReference().child("Purposes");

    saveBtn = findViewById(R.id.add_purpose_save_btn);

    saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String name = addNameEd.getText().toString();

            Map<String , String> purposeMap = new HashMap<>();
            purposeMap.put("name" , name);


            puposedRef.push().setValue(purposeMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(AddPurposeActivity.this, "Added to database", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(AddPurposeActivity.this, 
          task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }
    });

Retrieve class

        puposedRef = FirebaseDatabase.getInstance().getReference().child("Purposes");

    textView = findViewById(R.id.show_purpose);

    puposedRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            for(DataSnapshot ds : dataSnapshot.getChildren()){

                String key = puposedRef.push().getKey();
                String name = ds.child(key).getValue(Purpose.class).getName();
                textView.setText(name);

            }


        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            for(DataSnapshot ds : dataSnapshot.getChildren()){

                String key = puposedRef.push().getKey();
                String name = ds.child(key).getValue(Purpose.class).getName();
                textView.setText(name);

            }

        }


        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

logcat

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.best.karem.mycareassociatesadmin.Model.Purpose.getName()' on a null object reference
    at com.best.karem.mycareassociatesadmin.AddPurposeActivity$1.onChildAdded(AddPurposeActivity.java:52)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Karem Mohamed
  • 361
  • 4
  • 11

1 Answers1

0

You're attaching a ChildEventListener on Purposes. That means that its callback methods (onChildAdded, onChildChanged, etc) are called with a DataSnapshot of a child node of Purposes.

In your onChildAdded you're looping over the child nodes of that snapshot, which means that ds is a snapshot of an individual property name.

To fix the problem, don't loop over the child nodes in your onChild* methods:

public void onChildAdded(DataSnapshot dataSnapshot, String previousChildKey) {
    String name = dataSnapshot.getValue(Purpose.class).getName();
    textView.setText(name);
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807