2

This is the code that I'm using:

ref.get().addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
        DocumentSnapshot document = task.getResult();
        if (document.exists()) {
            Log.d(TAG, "DocumentSnapshot data: " + document.getData());
        }
    }
});

And this how Android Studio highlights the error:

enter image description here

As I understand from this answer:

A successfully completed task will never pass null for the DocumentSnapshot.

How can I solve this without checking for nullity? Thanks!

Apfelsaft
  • 5,766
  • 4
  • 28
  • 37
Johans Bormman
  • 855
  • 2
  • 11
  • 23
  • `A successfully completed task` returns never but task not completed successfully then what will become?? what can you expect?? – Tanveer Munir Feb 28 '19 at 12:17
  • @TanveerMunir I'm not sure I understand what you say. The task is returned successfully since I use `if (task.isSuccessful())`, right? – Johans Bormman Feb 28 '19 at 12:18
  • if `task.getResult()` might be returns null due to not have results but tasks completed then the `document` might be null so how can you Initialize? – Tanveer Munir Feb 28 '19 at 12:22
  • @TanveerMunir `if (task.isSuccessful())` it means that `task.getResult()` can never return `null`? It can only return an empty snapshot but **not** `null. Am I wrong? – Johans Bormman Feb 28 '19 at 12:29
  • 1
    The detection is limited. While it's never null if it's successful, the compiler doesn't know that. It sees a nullable return you don't check – Zoe Feb 28 '19 at 12:43
  • @TanveerMunir There is no place in the docs that says that it can return `null`. – Johans Bormman Feb 28 '19 at 12:45
  • @Zoe You say it cannot return `null` even if Android Studio complains about that? So it's an Android Studio issue and not a coding problem, right? – Johans Bormman Feb 28 '19 at 12:47
  • @JohansBormman assuming what you said about the bool check is true and correct, yes. – Zoe Feb 28 '19 at 12:54
  • @Andy lint checking is some times a part of the compiler – Zoe Feb 28 '19 at 12:56
  • As @Zoe said, it's limited. In your case, it can't be `null`. However, `DocumentSnapshot` can be a `null` object normally . And you are calling a method of an object that has the possibility of being `null` so you get the warning. To remove the warning, you can just change that `if` statement to something like `if(document != null && document.exists())` – ᴛʜᴇᴘᴀᴛᴇʟ Feb 28 '19 at 13:03
  • @ᴛʜᴇᴘᴀᴛᴇʟ I understand now, but as long as it cannot be null, I don't want to make another check without any reason but thanks anyway. – Johans Bormman Feb 28 '19 at 13:10

1 Answers1

4

Why does Android Studio indicate that an object might be null when it cannot be?

Because lint cannot know. document.exists() can return null in general and that's why you see the warning. Lint performs basic checks and doesn't know the details about the Firebase API. You can even reproduce this behaviour with the Java Core API.

How can I solve this without checking for nullity? Thanks!

If you are 100% sure that it can never be null, you can use

@SuppressWarnings("ConstantConditions")

But I would not recommend that, because you don't know if the Firebase API will change in the future. Maybe the next version of Firebase will allow null returns.

Apfelsaft
  • 5,766
  • 4
  • 28
  • 37