Android Question [Firestore lib] CollectionReference is not inited

peacemaker

Expert
Licensed User
Longtime User
Hi, @DonManfred

Lib is v.0.59. DB has the collection "users", but cannot get.

B4X:
Dim users As CollectionReference = fs.collection("users", "")
users.limit(10).fetch("UserFetch")
This is in your example, no error - but no records from db (exists 1).

B4X:
Dim users As CollectionReference = fs.collection2("users")
users.limit(10).fetch("UserFetch")

This does not work, seems not initialized, but users is not Null:
B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase(java.util.Locale)' on a null object reference
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
1. Post the full stacktrace
2. Post a small project which shows the issue. Hard to help with 2 lines of code not seeing anything other.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
1)
B4X:
Dim users As CollectionReference = fs.collection2("users")
users.limit(10).fetch("UserFetch")
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Setting query to get Users
Error occurred on line: 62 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase(java.util.Locale)' on a null object reference
    at de.donmanfred.Querywrapper.Initialize(Querywrapper.java:31)
    at de.donmanfred.CollectionReferencewrapper.limit(CollectionReferencewrapper.java:180)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at peacemaker.actmving.main.afterFirstLayout(main.java:104)
    at peacemaker.actmving.main.access$000(main.java:17)
    at peacemaker.actmving.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
** Activity (main) Resume **

2) The same your base example to get a collection, but real collection in db:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    fs.Initialize("Firestore",True)
    'Log(fs.FirestoreSettings)    'FirebaseFirestoreSettings{host=firestore.googleapis.com, sslEnabled=true, persistenceEnabled=true, timestampsInSnapshotsEnabled=true}
    Dim users As CollectionReference = fs.collection2("users")
    If users <> Null Then
        Log("Setting query to get Users")
        users.limit(10).fetch("UserFetch")
        Wait for UserFetch_Snapshot(success As Boolean, snap As QuerySnapshot, info As String)
        Log($"User_Snapshot(${snap.Size},${snap})"$)
        Log(snap.Metadata)
        Dim documents As List = snap.Documents
        Log(documents)
        If documents.IsInitialized Then
            If documents.Size > 0 Then
                For i = 0 To documents.Size-1
                    Dim docsnap As DocumentSnapshot = documents.Get(i)
                    Log("Query4DonManfred DocumentNo: #"&i&": "&docsnap.Id)
                Next
            End If
        Else
            Log("Snapshot does not contain a list of Documents...")
        End If
    End If

End Sub
Project attached. Collection "users" with 1 document is created in the db.

I just try to start to use the lib, but ... seems, it's hard or not so good documented.
 

Attachments

  • test_project.zip
    10.2 KB · Views: 345
  • TempDownload.png
    TempDownload.png
    65.3 KB · Views: 282
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
seems, it's hard or not so good documented.
It is not documented at all. You can find all the documentation on the Firebase site

Try this snippets please

B4X:
    fs.Initialize("Firestore",True)
    Dim users As CollectionReference = fs.collection("Users","UserCollection")
    If users <> Null Then
        Log("Setting query to get Users")
        'driver.whereEqualTo("id","DonManfred").limit(1).fetch("DriverFetch")
        users.setEventname("UserWatch","Userlist")
        'users.addSnapshotListener
        users.orderBy("Username").limit(25).fetch("UserWatch")
        wait for UserWatch_Snapshot(success As Boolean, snap As QuerySnapshot, info As String)
        Log($"UserWatch_Snapshot(${success},${snap.Size},${snap})"$)
        Dim meta As SnapshotMetadata = snap.Metadata
        Dim documents As List = snap.Documents
        'Log(documents)
        If documents.IsInitialized Then
            If documents.Size > 0 Then
                For i = 0 To documents.Size-1
                    Dim docsnap As DocumentSnapshot = documents.Get(i)
                    Log("Query DocumentNo: #"&i&": "&docsnap.Id)
                    Dim data As Map = docsnap.Data
                    Log(data)
                Next
            End If
        End If
    Else
        Log("Snapshot does not contain a list of Documents...")
    End If

notepakhex048.png


*** Service (starter) Create ***
Using FileProvider? true
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Setting query to get Users
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
UserWatch_Snapshot(true,1,com.google.firebase.firestore.QuerySnapshot@3c261e5c)
Query DocumentNo: #0: BfM4D5BtjxNYmSDrnJtC
(MyMap) {[email protected], Username=DonManfred}
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Project attached
note that i only can use it with my Database as my Sign SHA256 is not bound to your project. But i create a new collection Users and added one Entry there in the Console and then i tried to read the Users collection.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Sure, i understand that your and my DBs are different, all depends on the google-services.json, but ... i always got 0 documents :(

Do you use the access rights
B4X:
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Do you use the access rights
Honestly i did not had an entry here when testing the Users collection.

But i have it like this in my testdatabase

B4X:
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
  match /databases/{database}/Drivers {
    match /{document=**} {
      allow read, write;
    }
  }
  match /databases/{database}/Cars {
    match /{document=**} {
      allow read, write;
    }
  }
  match /databases/{database}/Pets {
    match /{document=**} {
      allow read, write;
    }
  }
 match /databases/{database}/Users {
    match /{document=**} {
      allow read, write;
    }
  }
}
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I always get
B4X:
UserWatch_Snapshot(true,0,com.google.firebase.firestore.QuerySnapshot@f3e820bf)
if try by
B4X:
users.orderBy("Username").limit(25).fetch("UserWatch")
or
B4X:
users.limit(25).fetch("UserWatch")

:(
How to debug the problem ?
All looks like working, but no data from the cloud.
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
@DonManfred , the project is sent to you.
And i see now the difference in your "Users" and my "users" collections.
But waiting for a test result from you (project is with your SHA1 now) - try "Users" and "users".
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
No, i try just your code example.
Now project is with your SHA1 - can you read my "users" from your side?
Did you try "Users" and "users" ?

If i start my example now (it's my SHA1) - the same, works, but 0 documents from "users" and from "Users".
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Querying
B4X:
Dim users As CollectionReference = fs.collection("users","UserCollection")
I get 2 events with a zero Snapshot

Even trying to add something to the users collection noting happens.

Can you post the rules used?

Try the following.
B4X:
service cloud.firestore {
 match /databases/{database}/users {
    match /{document=**} {
      allow read, write;
    }
  }
}
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
It was and is as you mentioned.
 

Attachments

  • TempDownload.png
    TempDownload.png
    22.9 KB · Views: 295
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It was and is as you mentioned.

Lets start completely new.

Based on your Screenshot i am expecting you do not have a users or even Users tree in your DB. Only "documents".

Please post a screenshot of the DB you are using.
Post the rules (as text) used for this Database structure.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I just try to start to use the lib
Based on the Screenshot in #3 of this thread you do have a users tree. But in your rules screenshot there is no rule for it.
If the Screen in #3 is correct then the rules should be
B4X:
service cloud.firestore {
 match /databases/{database}/users {
    match /{document=**} {
      allow read, write;
    }
  }
}
Start with this rule. Tell me if active and i´ll try again to fetch data.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Lets start completely new.
My db has:
1)
B4X:
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
The Simulator shows that "users" collection is accessible. And any other also, say "Users". I think no other rules needed.
Did you try the Simulator in your DB ?upload_2019-5-25_17-42-41.png upload_2019-5-25_17-47-0.png

upload_2019-5-25_17-42-41.png


2) No, "users" collection exists always, with 1 document named "demo".
upload_2019-5-25_17-47-0.png


I have added now your and my SHA-1 into the project, and guess now we can work together both sides (my JSON is new already).
And i have tried now with various rules - as you mentioned, 0 docs.
Now rules are so, try:
upload_2019-5-25_17-56-28.png
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Did you try the Simulator in your DB ?
No.

But i am using a real device and get access to all my collections.

I only can say that i could easily fetch the new Users Tree i added in the console and added a rule for.
All my rules are not having a "document" in the rules.
I saw similar rules when using the js framework. Don´t know if there is a difference.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
upload_2019-5-25_18-8-27.png

1) Simulator shows that no access if such rule.
2) Console allows to create "Users" collection also, together with "users" that exists.
 
Upvote 0
Top