How can I output Firebase data's in my text view items?

How can I output Firebase data's in my text view items?

I already have a Firebase database

enter image description here

public class Account extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "Petcare";
private MyPagerAdapter mMyPagerAdapter;
private ImageButton btn;
private TextView P_name;
private TextView P_address;
private TextView P_number;
private DatabaseReference mDB;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebasedatabase;
private FirebaseAuth.AuthStateListener firebaseauthlistener;
private String userId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account);
    btn = (ImageButton) findViewById(R.id.account_settings);

    P_name = (TextView) findViewById(R.id.profile_name);
    P_address = (TextView) findViewById(R.id.profile_address);
    P_number = (TextView) findViewById(R.id.profile_number);

    firebaseAuth = FirebaseAuth.getInstance();
    firebasedatabase = FirebaseDatabase.getInstance();
    mDB = firebasedatabase.getReference();
    FirebaseUser user = firebaseAuth.getCurrentUser();
    userId = user.getUid();


    mDB.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Showdata(dataSnapshot);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}
private void Showdata(DataSnapshot dataSnapshot) {
    for(DataSnapshot ds:dataSnapshot.getChildren()){
    UserInformation uinfo = new UserInformation();
    uinfo.setName(ds.child(userId).getValue(UserInformation.class).getName());
    uinfo.setContact(ds.child(userId).getValue(UserInformation.class).getContact());
    uinfo.setAddress(ds.child(userId).getValue(UserInformation.class).getAddress());

    //display
        P_name.setText(uinfo.getName());
        P_number.setText(uinfo.getContact());
        P_address.setText(uinfo.getAddress());

    }
}
}

I get an error

null 'java.lang.String com.example.jasper.happy_paws.UserInformation.getName()' on a null object reference at com.example.jasper.happy_paws.Account.Showdata(Account.java:130)

3 answers

  • answered 2018-11-08 08:33 Shehroz

    Try this code

    private void Showdata(DataSnapshot dataSnapshot) {
      P_name.setText(dataSnapshot.child(userId).child("name").getValue());
      P_number.setText(dataSnapshot.child(userId).child("contact").getValue());
      P_address.setText(dataSnapshot.child(userId).child("address").getValue());
    }
    

  • answered 2018-11-08 08:34 Alex Mamo

    To solve this, please use the following lines of code:

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef = rootRef.child("User").child(uid);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String address = dataSnapshot.child("address").getValue(String.class);
            String contact = dataSnapshot.child("contact").getValue(String.class);
            String name = dataSnapshot.child("name").getValue(String.class);
    
            P_address.setText(address);
            P_number.setText(contact);
            P_name.setText(name);
        }
    
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
        }
    };
    uidRef.addListenerForSingleValueEvent(valueEventListener);
    

  • answered 2018-11-08 11:09 Zameer Mullah Jojna

    databaseReference =FirebaseDatabase.getInstance().getReference("databasename"); 
    
       databaseReference.addListenerForSingleValueEvent(new ValueEventListener(){
            @Override
            public void onDataChange(DataSnapshot dataSnapshot){
                if (dataSnapshot.exists()){
                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
                  }else{
                      //do something here           
                 }}
            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(Allcontact.this, "" + databaseError, 
     Toast.LENGTH_SHORT).show();
            }
        });