简体   繁体   中英

linked list throws an IndexOutOfBoundsException

I have a piece of code which involve a LinkedList. The followings

topic.read() 
topic.delete() and 
topic.send() 

are methods from that LinkedList called Topic. These are being implemented in a GUI design. The methods

 topic.read(name) 
 topic.send(text) 

are working OK, but the

topic.delete(index) 

is throwing me an

IndexOutOfBoundsException

I explain the methods briefly:read(name) and send(text) take String parameters and reads the topics and its list of messages and sends messages to topics receptively. The delete(index) should delete the index-specified message from the topic. However, the error message is telling me that the Size is 0.

The relevant piece:(I reckon that the piece should be enough, if needed more pieces will be added)

public void act(String s)
{
    topic = new Topic(s, topics);
    if (s.equals("Read"))
        setEditorText(topic.read(readText()));
    else if (s.equals("Delete"))
        topic.delete(indexText());
    else if (s.equals("Send"))
    {   
        topic.send(getEditorText(), sendText());
        clear();
    }
}

Added these to this Quesion:

private JTextField indexText = new JTextField(10);
public int indexText()
{
    return Integer.parseInt(indexText.getText());
}

public class Topic {
    private LinkedList<String> messages = new LinkedList<String>();

    public void delete(int index)
    {   
    messages.remove(index - 1);
    }

}

You need to do bounds checking then, if the index is valid, before deleting, such as:

if (index > 0 && index <= messages.size()) {
    messages.remove(index - 1)
};

This will allow you to avoid IndexOutOfBoundsException

Hello Dilshat Abduwalli!

When you are getting a response that say your index size is 0 means that objects are not being added to the list or haven't yet been added and is why you go to delete said object of index value 2 for example it is going to throw an IndexOutOfBoundsException since the index is size is only 0. Ensure that you are adding values to your List or otherwise it will not be populated.

I would reccomend using @nitegazer2003 if statement you check for values that will fit within your List.size() you a integer isn't called that exceeds the List size which will give you the IndexOutOfBoundsException.

Double check your list values with a for loop.

for(int i = 0; i < list.size(); i++)
    System.out.println(list.get(i)); //Print the Strings in the list (Assuming its a list of Strings)

Or

for(int i = 0; i < list.size(); i++)
     System.out.println(list.getSize()); //Test the size of the list

Simular Question about Index 0 and OutOfBoundsException The last posted response explains a similar answer. You don't have to read all his code though.

Oracle's List Good source for documentation of List and its features.

I hope this helps or points you in the right direction! Good Luck!

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