Android Question [Firestore lib] CollectionReference is not inited

Discussion in 'Android Questions' started by peacemaker, May 23, 2019.

Tags:
  1. peacemaker

    peacemaker Well-Known Member Licensed User

    Hi, @DonManfred

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

    Code:
    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).

    Code:
    Dim users As CollectionReference = fs.collection2("users")
    users.limit(
    10).fetch("UserFetch")
    This does not work, seems not initialized, but users is not Null:
    Code:
    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: May 23, 2019
  2. DonManfred

    DonManfred Expert Licensed 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.
     
  3. peacemaker

    peacemaker Well-Known Member Licensed User

    1)
    Code:
    Dim users As CollectionReference = fs.collection2("users")
    users.limit(
    10).fetch("UserFetch")
    Code:
    *** 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:
    Code:
    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.
     

    Attached Files:

  4. DonManfred

    DonManfred Expert Licensed User

    what does that mean exactly? Can you describe it a bit more clear?
    thank you for the screenshot.
    I´ll check later today/this weekend.
     
    peacemaker likes this.
  5. DonManfred

    DonManfred Expert Licensed User

    It is not documented at all. You can find all the documentation on the Firebase site

    Try this snippets please

    Code:
    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
    [​IMG]

     
  6. DonManfred

    DonManfred Expert Licensed User

    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.
     
  7. peacemaker

    peacemaker Well-Known Member Licensed 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
    Code:
    service cloud.firestore {
      match /databases/{database
    }/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    ?
     
  8. DonManfred

    DonManfred Expert Licensed User

    Honestly i did not had an entry here when testing the Users collection.

    But i have it like this in my testdatabase

    Code:
    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;
        }
      }
    }
     
  9. peacemaker

    peacemaker Well-Known Member Licensed User

    I always get
    Code:
    UserWatch_Snapshot(true,0,com.google.firebase.firestore.QuerySnapshot@f3e820bf)
    if try by
    Code:
    users.orderBy("Username").limit(25).fetch("UserWatch")
    or
    Code:
    users.limit(25).fetch("UserWatch")
    :(
    How to debug the problem ?
    All looks like working, but no data from the cloud.
     
    Last edited: May 25, 2019
  10. DonManfred

    DonManfred Expert Licensed User

    Hard to tell not having access to your DB.

    Please add my signkey to your Firebaseproject (i´ll tell in a conversation).
    Create a small project configured for your Firebaseproject.
    Send me the Project AND your google-services.json in the conversation.
     
  11. peacemaker

    peacemaker Well-Known Member Licensed 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".
     
  12. DonManfred

    DonManfred Expert Licensed User

    I guess you now probably have a Users tree too :D
    with one entry of type Driver. Right?

    I´m just testing for querying users. Hold on
     
  13. peacemaker

    peacemaker Well-Known Member Licensed 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".
     
  14. DonManfred

    DonManfred Expert Licensed User

    Querying
    Code:
    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.
    Code:
    service cloud.firestore {
     match /databases/{database
    }/users {
        match /{document=**} {
          allow read, write;
        }
      }
    }
     
  15. peacemaker

    peacemaker Well-Known Member Licensed User

    It was and is as you mentioned.
     

    Attached Files:

  16. DonManfred

    DonManfred Expert Licensed User

    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.
     
  17. DonManfred

    DonManfred Expert Licensed User

    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
    Code:
    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.
     
  18. peacemaker

    peacemaker Well-Known Member Licensed User

    My db has:
    1)
    Code:
    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
     
  19. DonManfred

    DonManfred Expert Licensed User

    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.
     
  20. peacemaker

    peacemaker Well-Known Member Licensed 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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice