-1

I am building a simple message app, at the moment I would like to view the users image next to their text.
I am trying to get the users image from the firebase database.
However, whenever I try to getValue() of the an image the app crashes.
If I choose to replace imageURI with an image link, then I see it works with that link.

08-27 23:14:02.392 4817-4817/com.myproj.blogapp E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                         at com.myproj.blogapp.MessageAdapter$1.onDataChange(MessageAdapter.java:79)
                                                                         at com.google.android.gms.internal.to.zza(Unknown Source)
                                                                         at com.google.android.gms.internal.vj.zzHX(Unknown Source)
                                                                         at com.google.android.gms.internal.vp.run(Unknown Source)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5938)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)

@Override
public void onBindViewHolder(final MessageViewHolder viewHolder, final int index) {

    final Message c = messagesList.get(index);

    final String sender = c.getFrom();
    databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(sender);
    databaseReference.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            imageUrl = dataSnapshot.child("image").getValue().toString(); //error on this line
            viewHolder.setUserimage(context,imageUrl);


            String name = dataSnapshot.child("name").getValue().toString();
            viewHolder.displayName.setText(name);

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

    });

    viewHolder.messageText.setText(c.getMessage());
    viewHolder.time.setText(EpochtimeToDateAndTimeString(c.getTime()));
}

Database Image

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Your data is not complete. Look at the rust user. It has no image...

You must either use a proper null check after getChild(String path) before you toString it

Or use hasChild(String path) before getting it at all

In the case where the value is null or the child doesn't exist, you might want to consider having a default image

Note: you could be using getValue(String.class) on the children instead of using toString, or use a POJO on the whole initial Snapshot instead of fetching individual children

You'll also want to implement onCancelled so you can see your Firebase errors

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245