I met this error in the midst of my program output and it ends immediately. Only a total of 8 student objects were displayed. Below is a snippet of the output where the error was shown:
********************************************************************
********************************************************************
Student ID: 3
Student Title: Mrs
Student First Name: Sophie
Student Last Name: Chua
Student DOB: 3/3/2003
Assignment 1: 65
Assignment 2: 78
Practical: 7
Exam: 92
Overall: 81.6
Final Grade: HD
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
********************************************************************
********************************************************************
Student ID: 4
Student Title: Mrs
Student First Name: Brendon
Student Last Name: Low
Student DOB: 4/4/2004
Assignment 1: 95
Assignment 2: 98
Practical: 7
Exam: 69
at student.StudentClient.main(Overall: 80.1
StudentClient.java:420)
Final Grade: HD
********************************************************************
********************************************************************
Declaration of test array objects:
Student[] studentList = new Student[8];
studentList[0] = new Student();
studentList[1] = new Student();
studentList[2] = new Student();
studentList[3] = new Student();
studentList[4] = new Student();
studentList[5] = new Student();
studentList[6] = new Student();
studentList[7] = new Student();
studentList[8] = new Student();
studentList[9] = new Student();
studentList[0].setStudent("Mr", "Jason", "Lee", 1, 1, 1, 2001, 85, 75, 8, 65, 72.5, "D");
studentList[1].setStudent("Miss", "Candice", "Teo", 2, 2, 02, 2002, 95, 95, 9, 90, 92.0, "HD");
studentList[2].setStudent("Mrs", "Sophie", "Chua", 3, 3, 03, 2003, 65, 78, 7, 92, 81.6, "HD");
studentList[3].setStudent("Mrs", "Brendon", "Low", 4, 4, 04, 2004, 95, 98, 7, 69, 80.1, "HD");
studentList[4].setStudent("Mr", "Clarance", "Yeo", 5, 5, 05, 2005, 80, 76, 5, 59, 65.7, "C");
studentList[5].setStudent("Mr", "Adrian", "Tan", 6, 6, 6, 2006, 70, 60, 4, 20, 40.0, "ND");
studentList[6].setStudent("Ms", "Debbie", "Toh", 7, 7, 7, 2007, 60, 60, 7, 50, 56.0, "P");
studentList[7].setStudent("Miss", "Sarah", "Ho", 8, 8, 8, 2008, 59, 61, 7, 62, 62.0, "N");
studentList[8].setStudent("Mr", "Aloysius", "Lim", 9, 9, 9, 2009, 62, 63, 8, 69, 67.5, "C");
studentList[9].setStudent("Mrs", "Peggy", "Lo", 10, 10, 10, 2010, 65, 67, 9, 72, 71.4, "D");
BubbleSort method:
public static void BubbleSort(Student[] st) {
Student temp; //holding variable
boolean changed = false;
for (int j = 0; j < st.length - 1; j++) {
if (st[j] != null) {
long studentID1 = st[j].getStudentID();
if (st[j + 1] != null) {
long studentID2 = st[j + 1].getStudentID();
if ((st[j] != null) && (st[j + 1] != null)) {
if (studentID1 > studentID2) // change to > for ascending sort
{
temp = st[j]; //swap elements
st[j] = st[j + 1];
st[j + 1] = temp; //shows a swap occurred
changed = true;
}
}
}
}
}
if (changed) {
BubbleSort(st);
}
}
Main method:
BubbleSort(studentList);
for (int i = 0; i <= studentList.length; i++) {
if (studentList[i] != null) {
studentList[i].writeOutput();
}
}
I have not met this error before and I am unsure which part of my code that caused this error. The error was displayed when I ran a BubbleSort of student objects via Student ID and displaying them.
Please let me know if any more parts of my code will be required to troubleshoot this problem. Thank you
The length of the array doesn't match the number of elements you try to put in it.
Student[] studentList = new Student[8]; // change to 10
studentList[0] = new Student();
studentList[1] = new Student();
studentList[2] = new Student();
studentList[3] = new Student();
studentList[4] = new Student();
studentList[5] = new Student();
studentList[6] = new Student();
studentList[7] = new Student();
studentList[8] = new Student(); // 8 is an invalid index for an array of length 8
studentList[9] = new Student(); // 9 is an invalid index for an array of length 8
Also change
for (int i = 0; i <= studentList.length; i++)
to
for (int i = 0; i < studentList.length; i++)