d_vain
d_vain

Reputation: 25

Firestore - why my empty field is not empty?

I stumbled across a very strange error while I was doing my application. There is a function where on button click user can delete the whole Firestore documents AND the corresponding file from Storage. I store the whole URL for the picture in a string field inside the documents - but this url can be empty and this is my problem. I check if this URL field is null but somehow my code manages to skip this logical statement and tries to delete the file and that's where I get a nice nullpointer exception.

docRef = fStore.collection("ActiveJobs").document(docID);
    docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            if (documentSnapshot.exists()) {
                if (documentSnapshot.getString("jobPictureUrl") != null || documentSnapshot.get("jobPictureUrl) != null) {
                    String pictureUrl = documentSnapshot.getString("jobPictureUrl");
                    storageRef = fStorage.getReferenceFromUrl(pictureUrl);
                    storageRef.delete();
                }
            }
        }
    });

As you can see I do check if this field is empty or not in two different ways but somehow this statement always returns that it's not empty and my storageRef line gets a nullpointer exception, because the pictureUrl above IS empty. I tried to check if pictureUrl is empty, but still got the same error...

This is how I store the empty string field

Hope you can help me out and thanks for reading this!

Upvotes: 0

Views: 472

Answers (1)

Muthu Thavamani
Muthu Thavamani

Reputation: 1405

jobPictureUrl field is not a null member. It's an empty String.

So, you have to check whether the String is empty; but currently you are doing null check.

if (!documentSnapshot.getString("jobPictureUrl").isEmpty()) {
}

isEmpty() - Returns true if, and only if, string length() is 0

Upvotes: 1

Related Questions