Program: 34031
Project: 34031
Buggy source:
private void buildModelSpinner(ArrayList<Model> modelList,String selectedModel){
  if (selectedModel != null && !selectedModel.isEmpty()) {
    selectedModel=selectedModel.toLowerCase(Locale.UK);
  }
  if (modelMap == null) {
    modelMap=new TreeMap<>();
  }
  modelMap.clear();
  if (modelList == null) {
    modelSpinner.setEnabled(false);
  }
 else {
    if (modelList.size() == 0) {
      modelSpinner.setEnabled(false);
    }
 else {
      modelSpinner.setEnabled(true);
      for (      Model model : modelList) {
        try {
          modelMap.put(model.getId(),model.getName());
        }
 catch (        EvercamException e) {
          Log.e(TAG,e.toString());
        }
      }
    }
  }
  Collection<String> modelNameCollection=modelMap.values();
  String[] fullModelArray=Commons.joinStringArray(new String[]{getResources().getString(R.string.select_model)},modelNameCollection.toArray(new String[0]));
  ArrayAdapter<String> spinnerArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,fullModelArray);
  spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner);
  modelSpinner.setAdapter(spinnerArrayAdapter);
  int selectedPosition=0;
  if (selectedModel != null) {
    if (modelMap.get(selectedModel) != null) {
      String selectedModelName=modelMap.get(selectedModel);
      selectedPosition=spinnerArrayAdapter.getPosition(selectedModelName);
    }
  }
  if (selectedPosition != 0) {
    modelSpinner.setSelection(selectedPosition);
  }
 else   if (modelSavedSelectedPosition != 0 && modelSpinner.getCount() > 1) {
    modelSpinner.setSelection(modelSavedSelectedPosition);
    modelSavedSelectedPosition=0;
  }
 else {
    modelSpinner.setSelection(spinnerArrayAdapter.getPosition(getString(R.string.model_default)));
  }
}

Fixed version (ground truth):
private void buildModelSpinner(ArrayList<Model> modelList,String selectedModel){
  if (selectedModel != null && !selectedModel.isEmpty()) {
    selectedModel=selectedModel.toLowerCase(Locale.UK);
  }
  if (modelMap == null) {
    modelMap=new TreeMap<>();
  }
  modelMap.clear();
  if (modelList == null) {
    modelSpinner.setEnabled(false);
  }
 else {
    if (modelList.size() == 0) {
      modelSpinner.setEnabled(false);
    }
 else {
      modelSpinner.setEnabled(true);
      for (      Model model : modelList) {
        try {
          modelMap.put(model.getId(),model.getName());
        }
 catch (        EvercamException e) {
          Log.e(TAG,e.toString());
        }
      }
    }
  }
  Collection<String> modelNameCollection=modelMap.values();
  String[] fullModelArray=Commons.joinStringArray(new String[]{getResources().getString(R.string.select_model)},modelNameCollection.toArray(new String[0]));
  ArrayAdapter<String> spinnerArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,fullModelArray);
  spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner);
  modelSpinner.setAdapter(spinnerArrayAdapter);
  int selectedPosition=0;
  if (selectedModel != null) {
    if (modelMap.get(selectedModel) != null) {
      String selectedModelName=modelMap.get(selectedModel);
      selectedPosition=spinnerArrayAdapter.getPosition(selectedModelName);
    }
  }
  if (selectedPosition != 0) {
    modelSpinner.setSelection(selectedPosition);
  }
 else   if (modelSavedSelectedPosition != 0 && modelSpinner.getCount() > 1 && modelSavedSelectedPosition < modelSpinner.getCount()) {
    modelSpinner.setSelection(modelSavedSelectedPosition);
    modelSavedSelectedPosition=0;
  }
 else {
    modelSpinner.setSelection(spinnerArrayAdapter.getPosition(getString(R.string.model_default)));
  }
}