title: Realm Crashing on Moto G2 (Lollipop) but works on HTC Desire 620g (Kitkat)
body: <p>This is the function which throws me <strong>ArrayIndexOutOfBoundsException: rowIndex > available rows:</strong>. At line <code>if (!name.equalsIgnoreCase(realmResults.get(0).getName()) || !phoneNo.equalsIgnoreCase(realmResults.get(0).getPhone()) )</code>                                   </p>
  
  <p>The code runs fine on HTC 620g but crashes on Moto G2 and other devices.</p>
  
  <pre><code>public static void refreshingContactsDB(final Context context)
  {
      Thread background = new Thread(new Runnable()
      {
          public void run()
          {
              realmFresh = Realm.getInstance(context);
  
              createCountryDetailsArrayModel();
  
              TelephonyManager tm = (TelephonyManager) ApplicationController.getInstance()
                      .getSystemService(Context.TELEPHONY_SERVICE);
              String simCountryISO = tm.getSimCountryIso();
  
              for (int i = 0; i &lt; countryDetailsList.size(); i++) {
                  if (simCountryISO.equalsIgnoreCase(countryDetailsList.get(i)
                          .getCode())) {
  
                      dialCodePrefix = countryDetailsList.get(i).getDial_code();
  
                  }
              }
  
              AppPreferenceManager.getInstance().setContactUpdate(false);
  
              Logger.debug("Contact Update Refreshing Contact Started --&gt;");
  
  
              ContentResolver cr = ApplicationController.getInstance()
                      .getContentResolver();
  
              Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null);
  
              String duplicateName = "";
              String duplicatePhone = "";
  
              if (phones.getCount() &gt; 0) {
  
                  final int nameIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                  final int numberIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
  
                  while (phones.moveToNext()) {
                      String name = phones.getString(nameIndex);
                      String phoneNo = phones.getString(numberIndex);
  
                      if (phoneNo != null &amp;&amp; !phoneNo.isEmpty())
                      {
                          phoneNo = phoneNo.replace(" ", "");
                          phoneNo = phoneNo.replace("(", "");
                          phoneNo = phoneNo.replace(")", "");
                          phoneNo = phoneNo.replace("-", "");
                          phoneNo = phoneNo.replace("\u00a0", "");
                          phoneNo = phoneNo.replace("\u202c", "");
                          phoneNo = phoneNo.replace("\u2011", "");
                          phoneNo = phoneNo.replace("\u202a", "");
                          phoneNo = phoneNo.replace("*", "");
                          phoneNo = phoneNo.replace("#", "");
  
                          if (phoneNo.length() &gt;= 5) {
  
                              if (name.equalsIgnoreCase(duplicateName) &amp;&amp; phoneNo.equalsIgnoreCase(duplicatePhone)) {
                                  continue;
                              }
  
                              duplicateName = name;
                              duplicatePhone = phoneNo;
  
                              String formattedPhoneNumber;
                              formattedPhoneNumber = parseNumber(phoneNo);
  
                              RealmResults&lt;R_LocalContactDB&gt; realmResults = realmFresh.where(R_LocalContactDB.class).equalTo("phone", formattedPhoneNumber).findAll();
                              Logger.debug("Size: " + realmResults);
                              RealmResults&lt;R_LocalContactDB&gt; query = realmFresh.where(R_LocalContactDB.class).findAll();
                              R_LocalContactDB rContacts = new R_LocalContactDB(null, null, false, 0);
  
  
                              if (realmResults.size() == 0)
                              {
                                  i++;
                                  realmFresh.beginTransaction();
                                  R_LocalContactDB rCont = realmFresh.copyToRealm(rContacts);
                                  rCont.setName(name);
                                  rCont.setPhone(formattedPhoneNumber);
                                  rCont.setStatus(0);
                                  rCont.setMatchedWithRecent(true);
                                  Logger.debug("New Size: " + query.size());
                                  realmFresh.commitTransaction();
  
                                  Logger.debug("Sending To Server: " + i);
                                  Logger.debug("Contact Update " + name
                                          + " saved");
                              }
                              else
                              {
                                  realmFresh.beginTransaction();
                                  if (!name.equalsIgnoreCase(realmResults.get(0).getName()) || !phoneNo.equalsIgnoreCase(realmResults.get(0).getPhone()) )
                                  {
                                      realmResults.get(0).setName(name);
                                      realmResults.get(0).setPhone(phoneNo);
                                  }
  
  
                                  realmResults.get(0).setMatchedWithRecent(true);
                                  realmResults.get(0);
                                  Logger.debug("New Size Else Condition: " + query.size());
                                  realmFresh.commitTransaction();
  
  
                              }
                          }
                      }
                  }
                  ContactsSyncService.contactUpdated = false;
  
                  phones.close();
              }
              realmFresh.close();
  
              getNumbersFromDBAndUpdate();
              deleteExtraContacts();
          }
      });
      background.start();
  }
  </code></pre>