Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6900145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:30:34+00:00 2026-05-27T07:30:34+00:00

Working on a project for school and I’m getting a error when I try

  • 0

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;
}


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:30:35+00:00Added an answer on May 27, 2026 at 7:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a school project and I'm getting some weird errors from Xcode.
I am currently working on a project for school that is a java memo
I'm working on a school project which should give students the ability to search
I'm working on a project for school. That error appears when I tried to
I'm working on a school project, which requires JAVA to send a series of
I'm working on a school project and I'm running into an error that has
I am currently working on a project for school, it is Java based and
I'm working on a school project in which I try to simulate the network
I am working a school project to implement a Huffman code on text. The
I'm working on a project for school, and I'm implementing a tool which can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.