0

So am trying to retrieve values from firestore database, then store them into an Arraylist.There after am trying to choose the values at random from the array, then am updating a different field in the document ("GameState") while using the randomly selected value as a Key to update the ("GameState") field in the document. This what I have tried: But when I run it, the app crashes.

GamePortals.get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull @NotNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()){
            ArrayList<Integer> numberPot;
                        numberPot = new ArrayList<>();
                        int randomPicker = (int) (Math.random()*4);

                    for (QueryDocumentSnapshot document: task.getResult()){
                        //displayData.setText(String.valueOf(document.get("GameCode")));

                        
                        numberPot.add((Integer) document.get("GameCode"));
                        numberPot.get(randomPicker);

                        //displayData.setText(String.valueOf(numberPot));          
                        //displayData.setText(numberPot.get(randomPicker));
                       // Log.d(TAG,"onCreate: numbers:" + numberPot.get(randomPicker));

                        GamePortals.whereEqualTo("GameCode", numberPot.get(randomPicker))
                                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull @NotNull Task<QuerySnapshot> task) {
                                if (task.isSuccessful()){
                                    document.getReference().update("GameState","Winner");
                                    Toast.makeText(getApplicationContext(),"Updating was successful!", Toast.LENGTH_SHORT).show();
                                }else
                                {
                                    Toast.makeText(getApplicationContext(),"Updating was not successful!", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

            displayData.setText(String.valueOf(numberPot.get(randomPicker)));

                }else{
                    Toast.makeText(getApplicationContext(),"Some is wrong with Query", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

Clould Firestore Database

ERROR LOGCAT filtered by Error logcat image

LINE 83 (CODE SCREENSHOT ) Contains screenshot of the code(referenced in error logcat)

  • 2
    If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question. – Alex Mamo Sep 22 '21 at 10:29
  • If you post images of code or error messages, please also copy/paste or type the actual code/message directly into the post. While images and screenshots can be helpful for providing context, the post should still be clear and useful without them. Please see [Why may I not upload images of code on SO when asking a question?](//meta.stackoverflow.com/a/285557/208273)—the same reasoning applies to error messages as well. Posts in which essential text is only included in images are likely to be closed for not having enough details. – Ryan M Sep 22 '21 at 22:12

0 Answers0