Click here to Skip to main content
15,566,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get IndexOutOfBounds exception on my code on my MainActivity.class.

Index: 0 Size: 0

I tried to print out the coordinates I am receiving and it shows the actual coordinates I need. When I access the variable from another class, I am getting nothing. What seems to be the problem?

What I have tried:

I was trying to access coordinates from my ArrayList from Sample.class using:

This is my MainActivity.class

    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        LatLng latLng = new LatLng(14.397420, 121.033051);

        double lat = smsReceiver.locationcoords.get(0);
        double lon = smsReceiver.locationcoords.get(1);

            LatLng geofencePos = new LatLng(lat,lon);
            geofenceMarker = mMap.addMarker(new MarkerOptions()

This is my SmsReceiver.class where I am getting the coordinates. I printed the coordinates and it displays the coordinates I am receiving.

ArrayList locationcoords = new ArrayList(); //variable set on public

public void sendDataToFirebase(SmsMessage sms, Context context) {

databaseLocation = FirebaseDatabase.getInstance().getReference("smsdata");

String senderNumber = sms.getOriginatingAddress();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
String message = sms.getMessageBody();
String[] separatedSMS = message.split("\\s+");
String needHelp = "I NEED HELP";

    DatabaseReference mRef = databaseLocation.push();
    Double latitudedb = Double.parseDouble(separatedSMS[1]);
    Double longitudedb = Double.parseDouble(separatedSMS[3]);

    HashMap<String, Double> coordinates = new HashMap<String, Double>();
    coordinates.put("latitude", latitudedb);
    coordinates.put("longitude", longitudedb);


    double lat = locationcoords.get(0);
    double lon = locationcoords.get(1);

Updated 6-Apr-19 21:03pm

1 solution

We can't help you with that - we have no access to your data.
"Index out of bounds" means just that: the value supplied to access an array is outside the bounds of that array - either negative, or greater than or equal to the number of elements in the array.
But we have no idea which method is throwing the error, or what the parameter values you are passing to it are.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900