简体   繁体   中英

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 in Arraylist?

I have a listener, when i click on the dates it will search for the arraylist and get the dates of what i click then when i change the month i will removed the dates then add the dates in my current month.

public void onSelectDate(Date date, View view) {
    JSONObject jsonObj = new JSONObject(jsonStr);
    JsonArray datesIn = jsonObj.getJSONArray(TAG_SCHEDULE);
    for (int i = 0; i < datesIn.length(); i++) {
        JSONObject c = datesIn.getJSONObject(i);
        String dateD = c.getString(TAG_DATED);
        String statusD = c.getString(TAG_STATUS);
        HashMap<String, String> contact = new HashMap<String, String>();
        contact.put("Tag_dated", dateD);
        contact.put("Tag_status", statusD);
        contactList.add(contact);
        Date s = formatter.parse(dateD);

        ArrayList<HashMap<String, String>> contactList= new ArrayList<HashMap<String,String>>();;
        if(formatter.format(date).equals(contactList.get(0).get(TAG_DATED))){
            if(contactList.get(0).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(0).get(TAG_STATUS).equals("Reserved"))
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
        }else if(formatter.format(date).equals(contactList.get(1).get(TAG_DATED))){
            if(contactList.get(1).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(1).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else if(formatter.format(date).equals(contactList.get(2).get(TAG_DATED))){
            if(contactList.get(2).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(2).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else if(formatter.format(date).equals(contactList.get(3).get(TAG_DATED))){
            if(contactList.get(3).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(3).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else if(formatter.format(date).equals(contactList.get(4).get(TAG_DATED))){
            if(contactList.get(4).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(4).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else if(formatter.format(date).equals(contactList.get(5).get(TAG_DATED))){
            if(contactList.get(5).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(5).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(getApplicationContext(),"NP",Toast.LENGTH_SHORT).show();
        }
    }

my problem is when i change the month then click for the dates it has error.

public void onChangeMonth(int month, int year) {
    contactList.clear();
    HashMap<String, String> contact = new HashMap<String, String>();
    contact.put("Tag_dated", dateD);
    contact.put("Tag_status", statusD);
    contactList.add(contact);
}

Logcat error:

http://i.stack.imgur.com/Is35y.png

You're creating a new ArrayList called contactList , and adding one element to it; then you're trying to retrieve several elements from it!

Error is strait forward, size is two. Index starts at 0, so indexes available are 0 and 1 not 1 and 2. Your loop may need size - 1.

Instead of hardcoding your statements such as : if(formatter.format(date).equals(contactList.get(0).get(TAG_DATED))){

Try doing it in a loop where you go

public void updateToast(Date date, ArrayList<HashMap<String, String>> contactList) {
    String formattedDate = formatter.format(date);
    for (int i = 0; i < contactList.size(); i++)
    {
        if (formattedDate.equals(contactList.get(i).get(TAG_DATED))) 
        {
           if(contactList.get(i).get(TAG_STATUS).equals("Occupied"))
           {
              Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
              return;
           } else if(contactList.get(i).get(TAG_STATUS).equals("Reserved")) 
           {
              Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
              return;
           }
        }
    }
    // If here then its not found in any lists throw up your other text.
    Toast.makeText(getApplicationContext(),"NP",Toast.LENGTH_SHORT).show();
}

Some of this may be a bit out as its done free hand in here.

In your code onSelectDate you are saying:

contactList.add(contact);

It will add Hashmap into your list just once, so you could use contactList.get(0) on your Arraylist. But going forward you are saying

contactList.get(1);//even two

So it doesn't have element beyond first and hence it throws exception.

lets refactor your code little bit, lets get rid of this ugly nasty and unsafe if/else horror, and change it to nice and lovely loop

    boolean found = false;
    String formattedDate = formatter.format(date);
    for (int i = 0; i < contactList.size(); i++) {
        if (formattedDate.equals(contactList.get(i).get(TAG_DATED))) {
            if (contactList.get(i).get(TAG_STATUS).equals("Occupied")) {
                Toast.makeText(getApplicationContext(), "S",
                        Toast.LENGTH_SHORT).show();
            } else if (contactList.get(1).get(TAG_STATUS)
                    .equals("Reserved")) {
                Toast.makeText(getApplicationContext(), "R",
                        Toast.LENGTH_SHORT).show();
            }
            found = true;
            break;
        }
    }
    if (!found) {
        Toast.makeText(getApplicationContext(), "NP",
                Toast.LENGTH_SHORT).show();
    }

this code should do exactly same thing as yours but it shouldnt give you ArrayIndexOutOfBoundsException exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM