0
ArrayList aList = new ArrayList();

public void AddPerson() {
    String n = JOptionPane.showInputDialog(null, "Please Enter Name");
    String a = JOptionPane.showInputDialog(null, "Please Enter Name");
    String p = JOptionPane.showInputDialog(null, "Please Enter Name");
    PersonInfo person = new PersonInfo(n, a, p);
    aList.add(person);
}

public void Search(String n) {
    for (int i = 0; i <= aList.size(); i++) {
        PersonInfo person = (PersonInfo) aList.get(i);
        if (n.equals(person.name)) {
            person.PrintInfo();

        }

    }

}

public void remove(String n) {
    for (int i = 0; i <= aList.size(); i++) {
        PersonInfo person = (PersonInfo) aList.get(i);
        if (n.equals(person.name)) {
            aList.remove(i);

        }

    }

}
}

Search and remove functions don't work. I receive error message every time:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at lec06.AddressBook.Search(AddressBook.java:29) at lec06.Lec06.main(Lec06.java:33)

3
  • What is your PersonInfo class? Commented May 17, 2015 at 16:26
  • By the looks of it, perhaps you should be using i < aList.size() instead of i <= aList.size(). Commented May 17, 2015 at 16:28
  • Thanks sir Jo Liss its works thanks to all Commented May 18, 2015 at 19:01

4 Answers 4

3

Apparently your list is empty (did you use your AddPerson() function?).

In your for-loop you have following condition: i <= aList.size(). So even if aList size is equal to 0 (the list is empty), you'll try to get an element from that list (get(i)). That's why you get an exception.

You should change i <= aList.size() to i < aList.size() to solve this issue.

Sign up to request clarification or add additional context in comments.

1 Comment

@MuhammadZubairKHan: changing accepted answer every time someone ask for it (especially for the same answer answered later) is not the best practice.
2

For iterating through your lists in your add/remove functions, change the termination conditional to be <, strictly less than:

public void Search(String n) {
    for (int i = 0; i <= aList.size(); i++) {

And:

public void remove(String n) {
    for (int i = 0; i <= aList.size(); i++) {

This is the common way to iterate a list. Consider that lists (and arrays) in jave are zero-indexed, so the first element is at index 0, and the last element is at index size() - 1. So when you iterate to <= size() you exceed the length of the list, and generate the IndexOutOfBoundsException that you see.

Comments

2

Assuming every other thing is OK, change the condition in your for loop. You have written -

for (int i = 0; i <= aList.size(); i++) {

}  

Change it to -

for (int i = 0; i < aList.size(); i++) {

}  

You are getting the ArrayIndexOutOfBoutnd exception while you are trying to access the element at index aList.size. Because there is no element at aList.get(aList.size).
Array/ArrayList index starts at 0 and end at array.size-1.

Comments

2

Array, ArrayList, List starts form index 0.

So if the size of any of these above is 5, then the index for their items are 0 to 4.

What you are trying to do here is, you are trying to access the size index, meaning, if the size of the array list is 5, you are trying to access index 5, for (int i = 0; i <= aList.size(); i++) which does not exist. The last index should be size -1. And this is an ArrayIndexOutOfBound situation.

The correct code is :

for (int i = 0; i < aList.size(); i++)

1 Comment

@Muhammad :) Please mark it as answer if it solved your problem

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.