Search code examples
javaandroidexceptionarraylistconcurrentmodification

ConcurrentModificationException when I iterate on ArrayList


I want to iterate on an ArrayList called localWifiList that contains the wifi networks detected by a wifi scan. For every element of the ArrayList I want to run a query to get all tuples in the database with that specific mac address, create a new object and add this object into a new arrayList called wifiFromDatabase. I wrote this code:`

    ArrayList<wifiList> wifiFromDatabase = new ArrayList<wifiList>();
    ArrayList<wifiList> localWifiList = ScanService.wifiArraList;


    //field to read the values of wifi query results
    String mac;
    String ssid;
    String cid;
    String signalLevel;
    String capabilities;
    String rssi;
    String lat, lng;
    String date;
    String frequency;
    int flagInt;

    Cursor cursor;


    Iterator<wifiList> iterator = localWifiList.iterator();
    while(iterator.hasNext()){

        wifiList element = (wifiList) iterator.next();

        cursor = MainActivity.getDBOperationHelper().getWifiTupleByMac
                (MainActivity.getDBOperationHelper().getReadableDatabase(), element.getMacAddress());

        if(cursor.getCount()>0){

            if (cursor .moveToFirst()) {

                while (cursor.isAfterLast() == false) {
                    mac = cursor.getString(cursor.getColumnIndex(DBOperationHelper.MAC));//
                    ssid = cursor.getString(cursor.getColumnIndex(DBOperationHelper.SSID));//
                    capabilities = cursor.getString(cursor.getColumnIndex(DBOperationHelper.CAPABILITIES));//
                    frequency = cursor.getString(cursor.getColumnIndex(DBOperationHelper.FREQUENCY));//
                    cid = cursor.getString(cursor.getColumnIndex(DBOperationHelper.CELL_ID_UMTS));//
                    signalLevel = cursor.getString(cursor.getColumnIndex(DBOperationHelper.SIGNAL_LEVEL_WIFI));//
                    rssi = cursor.getString(cursor.getColumnIndex(DBOperationHelper.RSSI));
                    lat = cursor.getString(cursor.getColumnIndex(DBOperationHelper.GPS_LATITUDE_WIFI));//
                    lng = cursor.getString(cursor.getColumnIndex(DBOperationHelper.GPS_LONGITUDE_WIFI));//
                    date = cursor.getString(cursor.getColumnIndex(DBOperationHelper.DATE_WIFI));//
                    flagInt = cursor.getInt(cursor.getColumnIndex(DBOperationHelper.FLAG));



                    wifiList objectFromDb = WifiPhoneConfiguredNetworkHandler.CreateProperlyWifiListObject(ssid, capabilities, frequency, signalLevel, ConnectionPointAnalyzer.INVALID_ID_WIFI, signalLevel, 
                            mac, rssi, date, cid, lat, lng, flagInt, false);

                    wifiFromDatabase.add(objectFromDb);

                    cursor.moveToNext();
                }
            }

        }else{ //the database has not tuples with this mac

            Log.d(ConnectionPointAnalyzer.LOG_TAG, "OracoloBrain.java/AllInterfacesActived: no tuples found in the db with mac = "+element.getMacAddress()+
                    " ssid = "+element.getSsid());
        }

    } `

where the method CreateProperlyWifiListObject create an wifiList object given the fields passed as arguments. I read many thread about this issues, but nothing to do. I try also with synchronized on the arrayList. The exception is thrown by iterator.next() command.


Solution

  • Try to create a copy:

    ArrayList<wifiList> localWifiList = new ArrayList<wifiList>(ScanService.wifiArraList);