user9493136
user9493136

Reputation:

Firebase data returning null

Method

public void loadUserInformation() {
    final String mNumber = getActivity().getIntent().getStringExtra("Phone_Number");
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usesrRef = rootRef.child("Users").child(uid);
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            EditText Name = (EditText) rootView.findViewById(R.id.Name);
            ImageView profilePic = (ImageView) rootView.findViewById(R.id.profilePic);
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                String image = postSnapshot.child("Image").getValue().toString();

                Picasso.get().load(image).into(profilePic);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(), "Error Loading UserDetails", Toast.LENGTH_LONG).show();
        }
    };
    usesrRef.addListenerForSingleValueEvent(eventListener);
}

Null pointer exception on the String image = postsnapshot.child line...

Database structure:

enter image description here

Upvotes: 0

Views: 92

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Change this:

public void onDataChange(DataSnapshot dataSnapshot) {
        EditText Name = (EditText) rootView.findViewById(R.id.Name);
        ImageView profilePic = (ImageView)rootView.findViewById(R.id.profilePic);
        for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
            String image = postSnapshot.child("Image").getValue().toString();

           Picasso.get().load(image).into(profilePic);

        }
    }

into this:

public void onDataChange(DataSnapshot dataSnapshot) {
        EditText Name = (EditText) rootView.findViewById(R.id.Name);
        ImageView profilePic = (ImageView)rootView.findViewById(R.id.profilePic);

            String image = dataSnapshot.child("Image").getValue().toString();

           Picasso.get().load(image).into(profilePic);

        }

Your datasnapshot is at child(uid), then you do not need to loop since Image is a direct child for that snapshot.

Upvotes: 1

Related Questions