I have made a Journal app which uses Cloud Firestore and Firebase Auth.Everything was working great until I updated the security rules. After signout I come back to Login Activity and then immediately the app crashes and error is shown in previous page even after that activity is finished.Kindly help; Android Studio error message screenshot attached.
The App is crashing as soon as the user signs out
private void getJournals(final Context ctx) { final List<Journal> journalList=new ArrayList<>(); String currentUserId = currentUser.getUid(); collectionReference.whereEqualTo("userId",currentUserId) //line 182 .addSnapshotListener(new EventListener<QuerySnapshot>() { @RequiresApi(api = Build.VERSION_CODES.N) @Override public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) { if (!queryDocumentSnapshots.isEmpty()) { //line 187 for (QueryDocumentSnapshot snapshot : queryDocumentSnapshots) { Journal journal = snapshot.toObject(Journal.class); journal.setJournalId(snapshot.getId());//For updating we need a document reference journalList.add(journal); } journalList.sort(new JournalSorter()); Collections.reverse(journalList); journalRecyclerAdapter=new JournalRecyclerAdapter(ctx,journalList); recyclerView.setAdapter(journalRecyclerAdapter); journalRecyclerAdapter.notifyDataSetChanged(); } else{// Log.d(TAG,"User Data Empty"); } } }); }
Error Message after user signs out and crash:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.firebase.firestore.QuerySnapshot.isEmpty()' on a null object reference at com.name.myjournal.JournalListActivity$3.onEvent(JournalListActivity.java:187) at com.name.myjournal.JournalListActivity$3.onEvent(JournalListActivity.java:182) at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(Query.java:1038) at com.google.firebase.firestore.Query$$Lambda$3.onEvent(Unknown Source:6) at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(AsyncEventListener.java:42) at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(Unknown Source:6) at android.os.Handler.handleCallback(Handler.java:907) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7625) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)W/Firestore: (21.5.0) [Firestore]: Listen for Query(target=Query(Journal where userId == # com.google.firestore.v1.Value@4376bde5 integer_value: 0 string_value: "YXiu3zAEH7Qirmn3FCgjRhhxMbk1" order by __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}I/Process: Sending signal. PID: 15971 SIG: 9
My firebase security rules Android Studio:.`
service cloud.firestore { match /databases/{database}/documents { match /Journal/{JournalId}{ allow read:if request.auth!=null &&request.auth.uid==resource.data.userId; allow create:if isAuthenticated() &&request.auth.uid==request.resource.data.userId; allow update,delete:if isAuthenticated() &&request.auth.uid==resource.data.userId; } match /Users/{userId} { allow read,write:if request.auth!=null; } function isAuthenticated(){ return request.auth!=null; } }}