Program: 33593
Project: 33593
Buggy source:
private void updateQuery(final String query){
  currentQuery=query;
  String preparedQuery=Utilities.getPreparedQueryForSearch(query);
  ArrayList<ListItem> listItems=new ArrayList<>();
  Cursor teamQueryResults=Database.getInstance(this).getMatchesForTeamQuery(preparedQuery);
  if (teamQueryResults != null && teamQueryResults.moveToFirst()) {
    teamQueryResults.moveToPosition(-1);
    teamsHeader=new SearchResultsHeaderListElement(getString(R.string.teams_header));
    if (teamQueryResults.getCount() > MAX_RESULTS_PER_CATEGORY) {
      teamsHeader.showMoreButton(true);
      teamsHeader.setMoreCount(teamQueryResults.getCount() - MAX_RESULTS_PER_CATEGORY);
    }
 else {
      teamsHeader.showMoreButton(false);
    }
    listItems.add(teamsHeader);
    while (teamQueryResults.moveToNext()) {
      if (teamQueryResults.getPosition() >= MAX_RESULTS_PER_CATEGORY) {
        break;
      }
      String key=teamQueryResults.getString(teamQueryResults.getColumnIndex("String_Node_Str"));
      Team team=Database.getInstance(this).getTeamsTable().get(key);
      if (team == null) {
        team=new Team();
        team.setTeamKey(key);
        Database.getInstance(this).getTeamsTable().deleteSearchIndex(team);
        continue;
      }
      try {
        TeamListElement element;
        element=new TeamListElement(team);
        listItems.add(element);
      }
 catch (      BasicModel.FieldNotDefinedException e) {
        Log.e(Constants.LOG_TAG,"String_Node_Str" + Arrays.toString(e.getStackTrace()));
      }
      Log.d(Constants.LOG_TAG,"String_Node_Str" + teamQueryResults.getString(teamQueryResults.getColumnIndex(Database.SearchTeam.TITLES)));
    }
    teamQueryResults.close();
  }
 else {
    teamsHeader=new SearchResultsHeaderListElement(getString(R.string.teams_header));
    teamsHeader.showMoreButton(false);
    listItems.add(teamsHeader);
    listItems.add(new EmptyListElement(getString(R.string.no_teams_found)));
  }
  Cursor eventQueryResults=Database.getInstance(this).getMatchesForEventQuery(preparedQuery);
  if (eventQueryResults != null && eventQueryResults.moveToFirst()) {
    eventQueryResults.moveToPosition(-1);
    eventsHeader=new SearchResultsHeaderListElement(getString(R.string.events_header));
    if (eventQueryResults.getCount() > MAX_RESULTS_PER_CATEGORY) {
      eventsHeader.showMoreButton(true);
      eventsHeader.setMoreCount(eventQueryResults.getCount() - MAX_RESULTS_PER_CATEGORY);
    }
 else {
      eventsHeader.showMoreButton(false);
    }
    listItems.add(eventsHeader);
    while (eventQueryResults.moveToNext()) {
      if (eventQueryResults.getPosition() >= MAX_RESULTS_PER_CATEGORY) {
        break;
      }
      String key=eventQueryResults.getString(eventQueryResults.getColumnIndex("String_Node_Str"));
      Event event=Database.getInstance(this).getEventsTable().get(key);
      if (event == null) {
        event=new Event();
        event.setEventKey(key);
        Database.getInstance(this).getEventsTable().deleteSearchIndex(event);
        continue;
      }
      try {
        EventListElement element;
        element=new EventListElement(event);
        listItems.add(element);
      }
 catch (      BasicModel.FieldNotDefinedException e) {
        Log.e(Constants.LOG_TAG,"String_Node_Str" + Arrays.toString(e.getStackTrace()));
      }
    }
    eventQueryResults.close();
  }
 else {
    eventsHeader=new SearchResultsHeaderListElement(getString(R.string.events_header));
    eventsHeader.showMoreButton(false);
    listItems.add(eventsHeader);
    listItems.add(new EmptyListElement(getString(R.string.no_events_found)));
  }
  ListViewAdapter adapter=new ListViewAdapter(this,listItems);
  resultsList.setAdapter(adapter);
  resultsList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override public void onItemClick(    AdapterView<?> adapterView,    View view,    int position,    long id){
      ListViewAdapter adapter=(ListViewAdapter)adapterView.getAdapter();
      ListItem clickedItem=adapter.getItem(position);
      if (clickedItem instanceof TeamListElement) {
        String teamKey=((ListElement)clickedItem).getKey();
        Intent i=new Intent(SearchResultsActivity.this,ViewTeamActivity.class);
        i.putExtra(ViewTeamActivity.TEAM_KEY,teamKey);
        startActivity(i);
      }
 else       if (clickedItem instanceof EventListElement) {
        String eventKey=((ListElement)clickedItem).getKey();
        Intent intent=ViewEventActivity.newInstance(SearchResultsActivity.this,eventKey);
        startActivity(intent);
      }
 else       if (clickedItem == teamsHeader) {
        if (teamsHeader.isShowingMoreButton()) {
          startActivity(MoreSearchResultsActivity.newInstance(SearchResultsActivity.this,MoreSearchResultsActivity.TEAM_RESULTS,query));
        }
      }
 else       if (clickedItem == eventsHeader) {
        if (eventsHeader.isShowingMoreButton()) {
          startActivity(MoreSearchResultsActivity.newInstance(SearchResultsActivity.this,MoreSearchResultsActivity.EVENT_RESULTS,query));
        }
      }
 else {
        searchView.setVisibility(View.INVISIBLE);
        searchView.setVisibility(View.VISIBLE);
      }
    }
  }
);
}

Fixed version (ground truth):
private void updateQuery(final String query){
  currentQuery=query;
  String preparedQuery=Utilities.getPreparedQueryForSearch(query);
  ArrayList<ListItem> listItems=new ArrayList<>();
  Cursor teamQueryResults=Database.getInstance(this).getMatchesForTeamQuery(preparedQuery);
  if (teamQueryResults != null && teamQueryResults.moveToFirst()) {
    teamQueryResults.moveToPosition(-1);
    teamsHeader=new SearchResultsHeaderListElement(getString(R.string.teams_header));
    if (teamQueryResults.getCount() > MAX_RESULTS_PER_CATEGORY) {
      teamsHeader.showMoreButton(true);
      teamsHeader.setMoreCount(teamQueryResults.getCount() - MAX_RESULTS_PER_CATEGORY);
    }
 else {
      teamsHeader.showMoreButton(false);
    }
    listItems.add(teamsHeader);
    while (teamQueryResults.moveToNext()) {
      if (teamQueryResults.getPosition() >= MAX_RESULTS_PER_CATEGORY) {
        break;
      }
      String key=teamQueryResults.getString(teamQueryResults.getColumnIndex("String_Node_Str"));
      Team team=Database.getInstance(this).getTeamsTable().get(key);
      if (team == null) {
        team=new Team();
        team.setTeamKey(key);
        Database.getInstance(this).getTeamsTable().deleteSearchIndex(team);
        continue;
      }
      try {
        TeamListElement element;
        element=new TeamListElement(team);
        listItems.add(element);
      }
 catch (      BasicModel.FieldNotDefinedException e) {
        Log.e(Constants.LOG_TAG,"String_Node_Str" + Arrays.toString(e.getStackTrace()));
      }
      Log.d(Constants.LOG_TAG,"String_Node_Str" + teamQueryResults.getString(teamQueryResults.getColumnIndex(Database.SearchTeam.TITLES)));
    }
    teamQueryResults.close();
  }
 else {
    teamsHeader=new SearchResultsHeaderListElement(getString(R.string.teams_header));
    teamsHeader.showMoreButton(false);
    listItems.add(teamsHeader);
    listItems.add(new EmptyListElement(getString(R.string.no_teams_found)));
  }
  Cursor eventQueryResults=Database.getInstance(this).getMatchesForEventQuery(preparedQuery);
  if (eventQueryResults != null && eventQueryResults.moveToFirst()) {
    eventQueryResults.moveToPosition(-1);
    eventsHeader=new SearchResultsHeaderListElement(getString(R.string.events_header));
    if (eventQueryResults.getCount() > MAX_RESULTS_PER_CATEGORY) {
      eventsHeader.showMoreButton(true);
      eventsHeader.setMoreCount(eventQueryResults.getCount() - MAX_RESULTS_PER_CATEGORY);
    }
 else {
      eventsHeader.showMoreButton(false);
    }
    listItems.add(eventsHeader);
    while (eventQueryResults.moveToNext()) {
      if (eventQueryResults.getPosition() >= MAX_RESULTS_PER_CATEGORY) {
        break;
      }
      String key=eventQueryResults.getString(eventQueryResults.getColumnIndex("String_Node_Str"));
      Event event=Database.getInstance(this).getEventsTable().get(key);
      if (event == null) {
        event=new Event();
        event.setEventKey(key);
        Database.getInstance(this).getEventsTable().deleteSearchIndex(event);
        continue;
      }
      try {
        EventListElement element;
        element=new EventListElement(event);
        listItems.add(element);
      }
 catch (      BasicModel.FieldNotDefinedException e) {
        Log.e(Constants.LOG_TAG,"String_Node_Str" + Arrays.toString(e.getStackTrace()));
      }
    }
    eventQueryResults.close();
  }
 else {
    eventsHeader=new SearchResultsHeaderListElement(getString(R.string.events_header));
    eventsHeader.showMoreButton(false);
    listItems.add(eventsHeader);
    listItems.add(new EmptyListElement(getString(R.string.no_events_found)));
  }
  ListViewAdapter adapter=new ListViewAdapter(this,listItems);
  resultsList.setAdapter(adapter);
  resultsList.setOnItemClickListener((adapterView,view,position,id) -> {
    ListViewAdapter adapter1=(ListViewAdapter)adapterView.getAdapter();
    ListItem clickedItem=adapter1.getItem(position);
    if (clickedItem instanceof TeamListElement) {
      String teamKey=((ListElement)clickedItem).getKey();
      startActivity(ViewTeamActivity.newInstance(SearchResultsActivity.this,teamKey));
    }
 else     if (clickedItem instanceof EventListElement) {
      String eventKey=((ListElement)clickedItem).getKey();
      startActivity(ViewEventActivity.newInstance(SearchResultsActivity.this,eventKey));
    }
 else     if (clickedItem == teamsHeader) {
      if (teamsHeader.isShowingMoreButton()) {
        startActivity(MoreSearchResultsActivity.newInstance(SearchResultsActivity.this,MoreSearchResultsActivity.TEAM_RESULTS,query));
      }
    }
 else     if (clickedItem == eventsHeader) {
      if (eventsHeader.isShowingMoreButton()) {
        startActivity(MoreSearchResultsActivity.newInstance(SearchResultsActivity.this,MoreSearchResultsActivity.EVENT_RESULTS,query));
      }
    }
 else {
      searchView.setVisibility(View.INVISIBLE);
      searchView.setVisibility(View.VISIBLE);
    }
  }
);
}