title: Flag Quiz application (throwIndexOutOfBoundsException)
body: <p>Essentially what I made is a flag quiz app that tests how many flags you guess correctly and gives you a percentage based score at the end.</p>
  
  <p>The problem is that when I do the quiz I sometimes get an outOfBounds error and the app crashes instead of showing the score. It is pretty random because other times I can do the app 3-4 times no problems with the score displaying correctly at the end. </p>
  
  <p>These are the 2 classes used when resetting the quiz at the end, The problem is <code>String nextImage = quizCountriesList.remove(0);</code> inside of <code>loadNextFlag()</code></p>
  
  <p>the exact error is: </p>
  
  <blockquote>
    <p>at
    java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)</p>
    
    <p>at java.util.ArrayList.remove(ArrayList.java:403)</p>
    
    <p>at
    com.map524s1a.flagquizdk.MainActivityFragment.loadNextFlag(MainActivityFragment.java:180)</p>
  </blockquote>
  
  <pre><code>public void resetQuiz() {
      // use AssetManager to get image file names for enabled regions
  
      AssetManager assets = getActivity().getAssets();
      fileNameList.clear(); // empty list of image file names
  
      try {
          // loop through each region
          for (String region : regionsSet) {
              // get a list of all flag image files in this region
              String[] paths = assets.list(region);
  
              for (String path : paths)
                  fileNameList.add(path.replace(".png", ""));
          }
      } catch (IOException ex) {
          Log.e(TAG, "Error loading image file names", ex);
      }
  
      correctAnswers = 0; // reset the number of correct answers made
      questions = 0; // reset the total number of questions
      quizCountriesList.clear(); // clear prior list of quiz countries
  
      int flagCounter = 1;
      int numberOfFlags = fileNameList.size();
  
      // add FLAGS_IN_QUIZ random file names to the quizCountriesList
      while (flagCounter &lt;= FLAGS_IN_QUIZ) {
          int randomIndex = random.nextInt(numberOfFlags);
  
          // get the random file name
          String filename = fileNameList.get(randomIndex);
  
          // if the region is enabled and it hasn't already been chosen
          if (!quizCountriesList.contains(filename)) {
              quizCountriesList.add(filename); // add the file to the list
              ++flagCounter;
          }
      }
  
      loadNextFlag(); // start the quiz by loading the first flag
  }
  
  // after the user guesses a correct flag, load the next flag
  private void loadNextFlag() {
      // get file name of the next flag and remove it from the list
      String nextImage = quizCountriesList.remove(0);
  
      correctAnswer = nextImage; // update the correct answer
  
      answerTextView.setText(""); // clear answerTextView
  
      // display current question number
      questionNumberTextView.setText(getString(R.string.question, (questions + 1), FLAGS_IN_QUIZ));
  
      // extract the region from the next image's name
      String region = nextImage.substring(0, nextImage.indexOf('-'));
  
      // use AssetManager to load next image from assets folder
      AssetManager assets = getActivity().getAssets();
  
      // get an InputStream to the asset representing the next flag
      // and try to use the InputStream
      try (InputStream stream =
                   assets.open(region + "/" + nextImage + ".png")) {
          // load the asset as a Drawable and display on the flagImageView
          Drawable flag = Drawable.createFromStream(stream, nextImage);
          flagImageView.setImageDrawable(flag);
  
          animate(false); // animate the flag onto the screen
      }
      catch (IOException exception) {
          Log.e(TAG, "Error loading " + nextImage, exception);
      }
  
      Collections.shuffle(fileNameList); // shuffle file names
  
      // put the correct answer at the end of fileNameList
      int correct = fileNameList.indexOf(correctAnswer);
      fileNameList.add(fileNameList.remove(correct));
  
      // add 2, 4, 6 or 8 guess Buttons based on the value of guessRows
      for (int row = 0; row &lt; guessRows; row++) {
          // place Buttons in currentTableRow
          for (int column = 0;
               column &lt; guessLinearLayouts[row].getChildCount();
               column++) {
              // get reference to Button to configure
              Button newGuessButton =
                      (Button) guessLinearLayouts[row].getChildAt(column);
              newGuessButton.setEnabled(true);
  
              // get country name and set it as newGuessButton's text
              String filename = fileNameList.get((row * 2) + column);
              newGuessButton.setText(getCountryName(filename));
          }
      }
  
      // randomly replace one Button with the correct answer
      int row = random.nextInt(guessRows); // pick random row
      int column = random.nextInt(2); // pick random column
      LinearLayout randomRow = guessLinearLayouts[row]; // get the row
      String countryName = getCountryName(correctAnswer);
      ((Button) randomRow.getChildAt(column)).setText(countryName);
  }
  </code></pre>