1

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.

3
  • Is the code which populates localWifiList called from the same thread? Commented Aug 27, 2014 at 9:09
  • 1
    Some therad is probably modifying localWifiList while you are iterating over it. Thus the exception. A solution is to make a local copy of localWifiList and iterate over the copy. Commented Aug 27, 2014 at 9:12
  • localWifiList is populated with ScanService.wifiArraList that is in another class (a Service class). The exception is thrown in the piece of code above an no in the ScanService class. I don't use thread, after populating the local arraylist in ScanService class I call the method above. Commented Aug 27, 2014 at 9:15

1 Answer 1

1

Try to create a copy:

ArrayList<wifiList> localWifiList = new ArrayList<wifiList>(ScanService.wifiArraList);
Sign up to request clarification or add additional context in comments.

1 Comment

you are right, I'm very stupid, I was convinced to make a copy but ArrayList<wifiList> localWifiList = ScanService.wifiArraList doesn't make it. Thank you, now it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.