0

Exception in thread "main" javalangIndexOutOfBoundsException enter code here course class

public class Course implements Serializable {
    int id;
    String name;
    Subject subjects;
    Teacher teachers;
    List <User> students;
    List<Student> listOfStudentss = new ArrayList<Student>();
    public int getId() {
        return id;
    }

    public List<Student> getListOfStudentss() {
        return listOfStudentss;
    }

    public void setListOfStudentss(List<Student> listOfStudentss) {
        this.listOfStudentss = listOfStudentss;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Subject getSubjects() {
        return subjects;
    }

    public void setSubjects(Subject subjects) {
        this.subjects = subjects;
    }

    public Teacher getTeachers() {
        return teachers;
    }

    public void setTeachers(Teacher teachers) {
        this.teachers = teachers;
    }

    public List<User> getStudents() {
        return students;
    }

    public void setStudents(List<User> students) {
        this.students = students;
    }

    public Course(int id, String name) {
        this.id = id;
        this.name = name;
        this.subjects = subjects;
        this.teachers = teachers;
        this.students = students;
        this.listOfStudentss = listOfStudentss;
    }

    @Override
    public String toString() {
        return (" id: "+id+" name: " + name + "\n");
    }


    }

class Student


import java.io.Serializable;

public class Student extends User implements Serializable {
   String name;
   String surname;
   String group;

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getSurname() {
       return surname;
   }

   public void setSurname(String surname) {
       this.surname = surname;
   }

   public String getGroup() {
       return group;
   }

   public void setGroup(String group) {
       this.group = group;
   }

       public Student(int id, String login, String password, String name, String surname, String group) {
       super(id, login, password);
       this.name = name;
       this.surname = surname;
       this.group = group;
   }


   @Override
   public String getUserData() {
       return null;
   }
   public String toString() {
       return (" id: "+id+" login: " + login + " password: " + password+" name: "+name+" surname: "+surname+" group: "+group+"\n");
   }

}

 List<Admin> listOfAdmins = new ArrayList<Admin>();
       List<Student> listOfStudents = new ArrayList<Student>();
       List<Teacher> listOfTeachers = new ArrayList<Teacher>();
       List<Subject> listofSubjects = new ArrayList<Subject>();
       List<Course> listofCourses = new ArrayList<Course>();

       Scanner scan222 = new Scanner(System.in);
       System.out.print(" ENTER ID OF COURSE: ");
       listofCourses.forEach(System.out::print);
       int idCourse = scan222.nextInt();
       for (int i = 0; i < listofCourses.size(); i++) {
           if (listOfCourses.get(i).getId()==(idCourse)) {
                       System.out.print(" ENTER ID OF STUDENT: ");
                       listOfStudents.forEach(System.out::print);
                       int studentID = scan222.nextInt();
                       for (int j=0; j<listOfStudents.size(); j++)
                           if (listOfStudents.get(j).getId()==(studentID))  {
 //error in this line                          listofCourses.get(idCourse).listOfStudentss.add(listOfStudents.get(studentID));
                               }
                                 }
                           }
                           listofCourses.get(idCourse).listOfStudentss.forEach(System.out::print);
                           listofCourses.forEach(System.out::print);

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at RemR.main(RemR.java:93)

Isabay007
  • 3
  • 2
  • 1
    Which line is the error? – Devesh Kumar Singh Jun 09 '19 at 08:53
  • 2
    Can you give more details about the exception? In which line is it thrown? After a first look on your code, I would guess that it is the "listOfAdmins.get(i)" - your loop with i is going from 0 to listOfCourses.size() but you use it in listOfAdmins which easily gets such an exception if there are less Admins than Courses. – Konrad Neitzel Jun 09 '19 at 08:53
  • listofCourses.get(idCourse).listOfStudentss.add(listOfStudents.get(studentID)); this line – Isabay007 Jun 09 '19 at 08:56
  • That line has two possible reasons for that error: a) listOfStudents.get(studentID): you searched for the student with the loop on j and student j has the id you are looking for. So I think this should be listOfStudents.get(j). b) listofCourses.get(idCourse) could be the same. Maybe idCourse is just the visual id and not the element number in the List? – Konrad Neitzel Jun 09 '19 at 09:04
  • My suggestion to get readable code which is also easier for you to test and understand would be to introduce more methods. So for each list you could introduce a method to lookup an element with a given id and it will give you back the position inside the list. (Just as a start, more methods like that should follow as required). – Konrad Neitzel Jun 09 '19 at 09:10
  • listofCourses.get(i).listOfStudentss.add(listOfStudents.get(j)); after that exception diappeared – Isabay007 Jun 09 '19 at 09:17

1 Answers1

0

You're conflating two different things: A course ID and its index in the listOfCourses list.

Presumably, you've given your first course the id of '1', but its index in the listOfCourses list is '0'. The error is telling you that you're asking the listOfCourses object for the object at index '1' (which is the second object, as java starts counting at 0), but the list only contains a single object, hence, an exception occurs.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72