Hey guys, this is my first time on here so I don't know if my code is going to come out messy, so if it does I'm sorry. I'm new to java programming and I'm working on an assignment where I was given the main method and one constructor class, and I had to create the other constructor class myself. However when I run the program, the main method complains at the line:

And says "java.lang.ArrayOutOfBoundsException: 0" I'm not sure what this means, but here is my constructor class StudentCollection along with the main method if that helps. Could anyone tell me what I am doing wrong?

publicclass Lab8 {publicstaticvoid main (String[] args){// Scanner to read input from the keyboard
Scanner keyboard =new Scanner(System.in);int size =Integer.parseInt(args[0]);// use "2" when you run the program
StudentCollection collection =new StudentCollection(size);
Student s;// s is a Null reference at this pointString name, major;int credits;double gpa;int choice;DecimalFormat df =newDecimalFormat("0.00");String line;do{System.out.println();System.out.println("\t1. Add a new Student to the collection");System.out.println("\t2. Remove a Student from the collection");System.out.println("\t3. Display the students in the collection");System.out.println("\t4. Display statistics");System.out.println("\t5. Quit the program");System.out.print("\nYour selection: ");
choice =Integer.parseInt(keyboard.nextLine());switch(choice){case1:System.out.print("Enter the student's full name: ");
name = keyboard.nextLine();System.out.print("Enter the students gpa: ");
line = keyboard.nextLine();
gpa =Double.parseDouble(line);System.out.print("Enter the student's major: ");
major = keyboard.nextLine();System.out.print("Enter the number of credits completed by the student: ");
line = keyboard.nextLine();
credits =Integer.parseInt(line);
s =new Student(name, gpa, major, credits);
collection.addToCollection(s);break;case2:System.out.print("Enter the name of the student that is to be removed: ");
name = keyboard.nextLine();boolean result = collection.removeFromCollection(name);if(result)System.out.println("Student with name "+ name +" is successfully removed");elseSystem.out.println("There is no Student whose name is "+ name);break;case3:System.out.println(collection);break;case4:System.out.println("There are "+ collection.getCount()+" students in the collection");System.out.println("Average GPA of the students in the collection: "+
df.format(collection.getAverageGPA()));System.out.println("Average number of credits completed by the students "+"in the collection: "+ collection.getAverageCreditsCompleted());System.out.println();break;case5:System.out.println("Thank you for using my program. Good bye!");System.exit(0);// quit the programdefault:System.out.println("Illegal Choice. Try again");}// end of switch}while(choice !=5);}// main method}

Thank you, I appreciate the help.

March 27th, 2013, 07:30 PM

Norm

Re: Error message java.lang.ArrayIndexOutOfBoundsException: 0

Quote:

java.lang.ArrayOutOfBoundsException: 0

The error message says that the array is empty (it does NOT have an element at index 0).
The code needs to test if the array has one or more elements BEFORE trying to access the first element.
Use the array field: .length to test the length: if(theArray.length > 0) ...

March 27th, 2013, 07:42 PM

EDale

Re: Error message java.lang.ArrayIndexOutOfBoundsException: 0

Quote:

Originally Posted by Norm

Use the array field: .length to test the length: if(theArray.length > 0) ...

How will this stop the error message from happening? And where do I put this if statement?

March 27th, 2013, 07:45 PM

Norm

Re: Error message java.lang.ArrayIndexOutOfBoundsException: 0

Quote:

where do I put this if statement?

Test the array's length BEFORE trying to index into the array.

Quote:

How will this stop the error message from happening

If the array is not long enough, do NOT try to index into it.

March 27th, 2013, 07:54 PM

EDale

Re: Error message java.lang.ArrayIndexOutOfBoundsException: 0

Isn't it supposed to be empty at index 0? That's where I'm supposed to put my first student object. If I don't index into it, won't that just stop the execution of the program since the if statement won't be met? I'm sorry, I'm really new to this, I have trouble understanding most things with arrays.

March 27th, 2013, 08:06 PM

Norm

Re: Error message java.lang.ArrayIndexOutOfBoundsException: 0

The indexes for arrays start at 0 and go to the array length-1.
The first element in an array is at index 0.

Quote:

where I'm supposed to put my first student object.

You never posted the full text of the error message that shows what line the error happens on.
Look at the error message, get the source code line number and find the source line where the error happens.
What array is being indexed on that line? I don't know what that line has to do with student objects.

March 27th, 2013, 08:12 PM

EDale

Re: Error message java.lang.ArrayIndexOutOfBoundsException: 0

That is all the compiler tells me for the error sir, and it highlights the line

The code needs to test if the length of the args array is > 0. If it is NOT, the code should NOT try to access index 0.
A couple of solutions if the length < 1
Print out an error message to the user and exit the program.
Assign a default value to the size variable.