Android app keeps crashing at list.get(1)

Hi!

Yesterday, my app worked perfectly. Today, when I open it to use it again, the app keeps crashing at this line (last line of the code):

// Defining the beacons to look out for //
private static final Map<String, List<String>> PLACES_BY_BEACONS;
// beacon defintion
private BeaconManager beaconManager;
private Region region;

// TODO: replace "<major>:<minor>" strings to match your own beacons.
static {
    Map<String, List<String>> placesByBeacons = new HashMap<>();
    placesByBeacons.put("56738:65449", new ArrayList<String>() {{   // beet
        add("Beacon 1");
    }});
    placesByBeacons.put("61045:1044", new ArrayList<String>() {{    // pink
        add("Beacon 2");
    }});
    PLACES_BY_BEACONS = Collections.unmodifiableMap(placesByBeacons);
}

private List<String> placesNearBeacon(Beacon beacon) {
    String beaconKey = String.format("%d:%d", beacon.getMajor(), beacon.getMinor());
    if (PLACES_BY_BEACONS.containsKey(beaconKey)) {
        return PLACES_BY_BEACONS.get(beaconKey);
    }
    return Collections.emptyList();
}

/* ----------------------------------------------------------------------------------------------
                                            oncreate
   ---------------------------------------------------------------------------------------------- */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    state_textview = (TextView) findViewById(R.id.state);   // initialize textview
    final Button startButton = (Button) findViewById(R.id.start); // initialize button

    checkExternalMedia();   // check availability of sd card for reading and writing
    verifyStoragePermissions(this); // check if storage permission has been given

    /*
                                 ACTIVITY                                               */

    beaconManager = new BeaconManager(this);
    beaconManager.setRangingListener(new BeaconManager.RangingListener() {

        @Override
        public void onBeaconsDiscovered(Region region, List<Beacon> list) {
            if (!list.isEmpty()) {                             // upon discovery of beacons, do:

                // indentify beacons
                final Beacon beacon_0 = list.get(0);
                final Beacon beacon_1 = list.get(1); // THIS LINE, IT KEEPS CRASHING THE APP

Anyone with any idea why? ):

You’re trying to access a second element of a list which probably has only one element (:

Hi @heypiotr! Thanks for the reply, I really appreciate it! ^^

I’m confused as to why the app is not able to detect my second beacon… The Estimote app from playstore clearly shows 2 beacons being detected. I double checked the major/minor as well. Do you know if there are any reasons why this could be happening?

Can’t see calls to “startRanging” in the code snippet you’ve shared, can you show us that part? Maybe the answer is in there.