1

After call Firebase RecyclerView, it shows me this error. After to call model.getAvatarUrl().isEmpty() because return null. Could you please help me, I stuck here. I called online and offline method and it recently shows me this error.

FindFriendsActivity.java

adapter = new FirebaseRecyclerAdapter<User, FindFriendsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull final FindFriendsViewHolder viewHolder, final int position, @NonNull final User model)
            {
                final String usersIDs = getRef(position).getKey();

                UsersRef.child(usersIDs).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists())
                        {
                            viewHolder.userName.setText(model.getName());
                            viewHolder.userStatus.setText(model.getStatus());


                            //Set Image
                            /*
                            if (model.getAvatarUrl().isEmpty()) {
                                viewHolder.profileImage.setImageResource(R.drawable.profile_image);
                            } else
                            {
                                Picasso.with(getBaseContext()).load(model.getAvatarUrl()).into(viewHolder.profileImage);
                            }*/
                            if (model.getAvatarUrl().isEmpty()){   //Error in this line
                                viewHolder.profileImage.setImageResource(R.drawable.profile_image);
                            }
                            else {
                                Picasso.with(getBaseContext()).load(model.getAvatarUrl()).into(viewHolder.profileImage);
                            }

                            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view)
                                {
                                    String visit_user_id = getRef(position).getKey();

                                    Intent profileIntent = new Intent(FindFriendsActivity.this, ChatActivity.class);
                                    profileIntent.putExtra("visit_user_id", visit_user_id);
                                    profileIntent.putExtra("visit_user_name", model.getName());
                                    profileIntent.putExtra("visit_image", model.getAvatarUrl());
                                    startActivity(profileIntent);
                                }
                            });

                        }
                    }

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

                    }
                });

            }

            @NonNull
            @Override
            public FindFriendsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
            {
                View itemView = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.users_display_layout, viewGroup, false);
                return new FindFriendsViewHolder(itemView);
            }
        };
  • You can use `StringUtils.isEmpty(CharSequence cs);` from [org.apache.commons.lang3](https://mvnrepository.com/artifact/org.apache.commons/commons-lang3). It will return `true` if the CharSequence is null or empty. – tgallei Mar 11 '20 at 06:37

1 Answers1

6

You can't call a method on null because null isn't an object and hasn't any methods. You need to nullcheck your value first either directly

model.getAvatarUrl() == null || model.getAvatarUrl().isEmpty()

or by using apache commons with

StringUtils.isEmtpy(model.getAvatarUrl())

.

Nicktar
  • 5,548
  • 1
  • 28
  • 43
  • [ StringUtils.isBlank(" ") = true ] ...just a words of CAUTION with StringUtils. It will trim your argument string. – Aditya Rewari Mar 11 '20 at 07:19
  • @AdityaRewari I don't know which Stringutils.isBlank() you're refering to but the one from `org.apache.commons.lang3` only calls `Character.isWhiteSpace()` on every character. – Nicktar Mar 11 '20 at 08:03
  • Yes. I just mentioned, that if only WhiteSpaces are passed, that will be marked as Blank. i.e.,true ! – Aditya Rewari Mar 11 '20 at 08:23
  • @AdityaRewari that's what `isBlank` is for, to check if the String contains only whitespaces. To check for an empty String, you should use `isEmpty`. – Nicktar Mar 11 '20 at 08:28
  • Yes.. I just mentioned this, as the 2 answer options given by you, will differ for a String which has only whitespaces. It was just a mention for Habner(guy who asked the question) !!! :) – Aditya Rewari Mar 11 '20 at 08:31
  • @AdityaRewari No, they don't because my answer uses `isEmpty` and not `isBlank`. – Nicktar Mar 11 '20 at 08:33
  • Ohh man.. made a mess !!! ..was using isBlank all this time. Sorry ! – Aditya Rewari Mar 11 '20 at 09:26