Dave
Dave

Reputation: 131

Finding index out of bounds exception

All was well with this program until I made some changes in my addMainMenu method. Now it seems as though there is an array index out of bounds somewhere. Eclipse is not leading me too it. Does anyone know why this code has an array index out of bounds exception.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.LinkedList.checkElementIndex(Unknown Source)
    at java.util.LinkedList.get(Unknown Source)
    at Menu.main(Menu.java:58)

import java.util.LinkedList;
import java.util.List;

public class Menu {
    LinkedList <LinkedList> mainMenuItems = new LinkedList <LinkedList> ();

    public void  Menu(){}

    public boolean addMainMenuItem(String newItem, String existingItem, int position){
        LinkedList <String> subMenuItems = new LinkedList <String> ();
        for (int i = 0; i<= mainMenuItems.size(); i++){
            if (mainMenuItems.get(i).contains(existingItem)){
                subMenuItems.addLast(newItem);
                int existingIndex = mainMenuItems.indexOf(existingItem);
                if (position == 1){
                    mainMenuItems.add(existingIndex + 1, subMenuItems);
                    break;
                }
                if (position == -1){
                    mainMenuItems.add(existingIndex, subMenuItems);
                    break;
                }
                if (i == mainMenuItems.size()){
                    subMenuItems.addLast(newItem);
                    mainMenuItems.add(subMenuItems);
                    break;
                }
            }
        }
    }
    return true;
}

public boolean deleteMainMenuItem(String item){
    if (mainMenuItems.contains(mainMenuItems.indexOf(item))){
        mainMenuItems.remove(mainMenuItems.indexOf(item));
        return true;
    }
    else{
        return false;
    }
}

public static void main(String[] args){
    Menu b = new Menu();
    b.addMainMenuItem("h", "b", 1);
    b.addMainMenuItem("hi", "h", 1);
    b.addMainMenuItem("i", "h", 1);
    System.out.println(b.mainMenuItems.get(0));
    b.deleteMainMenuItem("hi");
    System.out.println(b.mainMenuItems.get(0));
    System.out.println(b.deleteMainMenuItem("hi"));
}

Upvotes: 0

Views: 2930

Answers (3)

Tirath
Tirath

Reputation: 2404

First - Change the for loop as shown below

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

Also, you need to restructure addMainMenuItem method as shown below -

for (int i = 0; i < mainMenuItems.size(); i++){
    if (mainMenuItems.size() == 0) {
        //you should put list initialization code here
        //means, logic when there is nothing in the mainMenuItems list
    } else {
        //when the list is not empty
    }
}

You are getting IndexOutOfBoundsException because you are doing get(index) on mainMenuItems - which is empty in your for loop.

For example -

List<String> list = new LinkedList<String>();
list.get(0);    //will throw the same exception

Upvotes: 0

Kick Buttowski
Kick Buttowski

Reputation: 6739

There are two possible issues

1. In this line

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

you should have use i < mainMenuItems.size()

2. when you have not assigned any value to your LinkedList, you try to access an index of your LinkedList

Upvotes: 2

Nir Alfasi
Nir Alfasi

Reputation: 53525

Change the <= to < in: i<= mainMenuItems.size()

EDIT:
If mainMenuItems is still empty, the line mainMenuItems.get(i).contains(existingItem) will generate java.lang.IndexOutOfBoundsException because mainMenuItems.get(i) doesn't exist.

Upvotes: 1

Related Questions