Search code examples
javaandroidfirebasepicasso

Picasso "Target must not be null" (using Firebase database)


I made a Firebase database with links (these are links to my Storage with some images)

enter image description here

The Storage:

enter image description here

I try to connect to my database and get the value of Page_1 for example by pushing a button

 private DatabaseReference mRef;

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRef = FirebaseDatabase.getInstance().getReference().child("Chapters").child("Chapter 1").child("Page_1");

                mRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        String value = String.valueOf(dataSnapshot.getValue());

                        textView.setText(value);
                        Picasso.get().load(value).into(page_holder);
                    }

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

                    }
                });

            }
        });

But when I run my app there is an exception and Picasso line says that:

Target must not be null.

If I remove my picasso and only place value of Page_1 into textView it shows the correct value (so it means that I actually get my value and it is not null)

enter image description here

(sorry for the black pattern, I think that the lines might contain something important like a name of database (most likely not but I am a noob in stuff like that))

PS The one thing I noticed is that when I get my value of Page_1 into TextView there is a little delay there. So I guess that Picasso is trying to get "Target" before it is loaded. I tried to make something like a checker

String value_from_database = textView.getText().toString();
    if (TextUtils.isEmpty(value_from_database)){
                       textView.setText("Image is loading|Doesn't exist");
                    }
                    else {
                        Picasso.get().load(value_from_database).into(page_holder);
                    }

But I still fail. And Picasso shows the same exception/error.

Please, tell me how can I solve it?


Solution

  • As the error says:

    "Target must not be null."

    This means that in your case, the target is the page_holder, which is null. It has nothing to do with the code that you are using to get the data from the database, which is correct.

    To solve this, please make sure you initialize your page_holder object so it cannot be null and you can use Picasso correctly.