Question

Working on a project for school and I'm getting a error when I try to enter the number of students for the array. The error is

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Project1.enterStudents(Project1.java23)
at Project1.mainMenu(Project1.java59)
at Project1.enterStudents(Project1.java7)

The code I have written it below as always any help is appreciated.

import java.util.Scanner;

public class Project1{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Project1 project1 = new Project1();
project1.mainMenu();


}//main

int numOfStudents;
Student[] students = new Student[numOfStudents];

public void enterStudents(){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter number of students");
    numOfStudents = input.nextInt();
    int i;
    for(i = 0; i <= numOfStudents - 1; i++){
        i--;
        System.out.println("Enter student's ID: ");
        students[i].getId();
        System.out.println("Enter student's first name: ");
        students[i].getFirst();
        System.out.println("Enter student's last name: ");
        students[i].getLast();
        System.out.println("Enter student's class: ");
        students[i].getStuClass();
    }


}

public void retrieveStuId(){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter student id");


}

public void Exit(){
    System.exit(0);
}

public void mainMenu(){
    Scanner input = new Scanner(System.in);
    System.out.println("1 - Enter student info");
    System.out.println("2 - Retrieve student by ID");
    System.out.println("3 - Retrieve student by last name");
    System.out.println("4 - Update student");
    System.out.println("5 - Exit");
    int menuSelect = input.nextInt();

    if (menuSelect != 1 && menuSelect != 2 && menuSelect != 3 && menuSelect != 4 && menuSelect != 5)
        System.out.println("That is not a option");
    else
        switch (menuSelect){
            case 1: enterStudents();

            case 2: System.out.print("case 2");

            case 3: System.out.print("case 3");

            case 4: System.out.print("case 4");

            case 5: Exit();

        }
}

}//project1

class Student{
private int studentID;
private String firstName;
private String lastName;
private String stuClass;

public Student(){
}

public Student(int id, String first, String last, String c ){
    studentID = id;
    firstName = first;
    lastName = last;
    stuClass = c;
}

public void setID (int id){
    studentID = id;
}

public void  setStuClass (String c){
    stuClass = c;
}

public void setFirst(String first){
    firstName = first;
}

public void setLast(String last){
    lastName = last;
}

public String getFirst(){
    return firstName;
}

public String getLast(){
    return lastName;
}

public int getId(){
    return studentID;
}

public String getStuClass(){
    return stuClass;
} 

public String toString(){
    return "Student ID: " + studentID + " ---- " + "Student Name: " + firstName + "" + lastName + " ---- " + "Class:" + stuClass;
}


}
Was it helpful?

Solution

Look at this bit of code:

for(i = 0; i <= numOfStudents - 1; i++){
    i--;
    System.out.println("Enter student's ID: ");
    students[i].getId();

Now work out what the value of i is going to be on each line...

Why do you have the i--; line at all?

Note that this only addresses the first ArrayIndexOutOfBoundsException issue - once that's fixed, you'll end up with another ArrayIndexOutOfBoundsException because you're initializing the array before asking for numOfStudents.

Once that's addressed, you'll get a NullPointerException because you're trying to call methods via null references - you never actually create a new Student instance.

To be honest, this program is quite a long way from working - I'm not sure that Stack Overflow is going to provide the most effective teaching environment in this particular case. I would suggest you talk to your teacher and ask for some 1-on-1 tutoring.

OTHER TIPS

The i--; at the top of your loop body is a likely culprit. In fact, if it wasn't causing this problem, I think it'd make your loop run forever. Why is that even there?

Also, I see another problem. When the array students is initialized, numOfStudents has not yet been assigned a value. Since it's an instance variable, it defaults to 0, which means students won't actually hold any Students.

for(i = 0; i <= numOfStudents - 1; i++){
// REMOVE THIS        i--;
// i is = -1 here but Arrays start by 0
    System.out.println("Enter student's ID: ");
    students[i].getId();
    System.out.println("Enter student's first name: ");
    students[i].getFirst();
    System.out.println("Enter student's last name: ");
    students[i].getLast();
    System.out.println("Enter student's class: ");
    students[i].getStuClass();
}

Why this?

for(i = 0; i <= numOfStudents - 1; i++){
        i--; //<--THIS

The problem is probably there. You are tryng to access to an array with a negative index number.

int numOfStudents is initialized to nothing. so attempting to edit your students array will not work.

How to fix it:

Scanner input = new Scanner(System.in);
System.out.println("Enter number of students"); 
int numOfStudents = input.nextInt(); //this initializes to something.
Student[] students = new Student[numOfStudents];

then continue with your loop gathering the data.

Of course, you will want to remove the i-- as pointed out by others.

It's the i-- in your loop in enterStudents. This causes the index to go from 0 to -1 which is invalid. What exactly is this i-- supposed to accomplish?

Looks like others here have already pointed out the answer, but here's how to fish...

The fact that it's an ArrayIndexOutOfBoundsException tells you that it's happening while you're accessing an array, and it's even kind enough to tell you the index you've used that's out of bounds (in this case, -1, which is always out of bounds -- array indexes must be >= 0).

enterStudents only uses one array, and its index always comes from one variable. So, mentally step through your code and follow the values of that variable i to see if/how it can ever be -1.

Btw, for(i = 0; i <= numOfStudents - 1; i++) will work, but for(i = 0; i < numOfStudents; i++) is a bit more idiomatic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top