-2

I've been trying to implement a add function in the ListToDo class using three other classes Task, Date and Time. However once the user inputs the data into the add function the following happens;

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:665) at java.util.ArrayList.add(ArrayList.java:477) at ToDoList.addTask(ListToDo.java:213) at ToDoList.displayMenu(ListToDo.java:272) at ToDoList.main(ListToDo.java:188)

class ListToDo{
    private ArrayList<Task> list;
    public static void main(String[] args){


    menu(); // displays menu to user

}
public ListToDo(){
    list = new ArrayList<Task>();

}

public void add(Task newTask){      
    list.add(newTask.getTaskID(), newTask);
}

public void print(Task[] newTask){
    list.addAll(Arrays.asList(newTask));
    list.size();

    menu();
}


private static void menu(){
    Scanner input = new Scanner(System.in);
    ListToDo toDoList = new ListToDo();

    System.out.println("1: Add a task");

    int option=input.nextInt();

    switch(option){ // selection statement 
        case 1: //1: Add a task
        System.out.print("Please input the new task title: ");
        String title=input.next();

        System.out.print("Please input the new task date (dd mm yyyy): ");
        int day=input.nextInt();
        int month=input.nextInt();
        int year=input.nextInt();

        System.out.print("Please input the starting time (hh mm): ");
        int hour=input.nextInt();
        int min=input.nextInt();

        System.out.print("Please input the location: ");
        String location=input.next();

        Time time = new Time(hour, min);
        Date date = new Date(day, month, year);         

        Task newTask = new Task(time,date,title,location);

        toDoList.add(newTask);

        System.out.println("Task "+newTask.getTaskID()+" is added. The To-Do list is follows:");
        Task[] newTask1 = new Task[0];
        newTask1[0] = new Task(time,date,title,location);

        toDoList.print(newTask1);
        menu();
        break;



        default: //ERROR 
        System.out.println("ERROR! Please select an action listed below:");
        menu();


    } 

}

}

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    **Moderator Note**: Please do not vandalize your posts. Once you post a question, they belong to the site and its users. Even if it is no longer useful to you, it might be helpful to someone in the future. The answerers would have also put an effort in writing their answer, which would no longer be useful if you have removed the content from the post. Also, note that by posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted. – Bhargav Rao May 18 '18 at 10:49

1 Answers1

3

Te culprit lies in the detail. Let's take a look at the signature of List#add. In your method

public void add( Task newTask )
{
  list.add( newTask.getTaskID(), newTask );
}

you're passing two argument and therefore attempt to insert an element into a list rather than to append it.
However since your list is empty and newTask.getTaskId() returns a value greater than 0 the add method tries to insert your value at an index which is out of bounds and therefore causes the exception.

You should consider to simply add the task:

public void add( Task newTask )
{
  list.add( newTask );
}

and take a look at overloading since it's the main reason for your issue but also a powerfull tool if used correctly.

L.Spillner
  • 1,772
  • 10
  • 19