0

I'm writing a simple Java code and I'm getting this error after entering the first input:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at university.GetStudentSpect(university.java:26)
at university.main(university.java:11)

Code:

import java.util.Scanner;
public class university {
    public static Scanner Reader = new Scanner(System.in);
    public static int n;
    public static int m=0;
    public static int l;
    public static StringBuilder SConverter = new StringBuilder();
    public static void main(String[] args) {
        GetStudentsNumber();
        GetStudentSpect();
    }

    public static void GetStudentsNumber() {
        System.out.println("enter the number of students");
        n = Reader.nextInt();
    }
    public static String [][] StudentSpect = new String [n][2];

    public static void GetStudentSpect() {
        for (int i=0;i<n;i++) {
            System.out.println("enter the name of the student");
            StudentSpect[i][0] = SConverter.append(Reader.nextInt()).toString();
            System.out.println("enter the id of the student");
            StudentSpect[i][1] = SConverter.append(Reader.nextInt()).toString();
            System.out.println("enter the number of courses of the student");
            l = Reader.nextInt();
            m += l;
            StudentSpect[i][2] = SConverter.append(l).toString();
        }
    }
}

2 Answers 2

1

Static code gets executed when the class is first loaded. That means, you initialize StudentSpec before your main method runs. That in turn means that n is not assigned a value yet, so it defaults to 0. Because of that, StudentSpec is an array of dimensions zero by two. (Note that it does not matter whether you put the code to initialize StudentSpec with all the other variables or later in the class, all static stuff gets initialized first.)

Then your code in main runs, calling GetStudentsNumber, which sets n, but does not initialize StudentSpec (again). Then GetStudentSpect runs, and as soon as you try to access StudentSpec, your program crashes, as it's an array with zero elements.

To fix this, initialize StudentSpec in GetStudentsNumber after reading n, i.e., move the code from the static initializer to this method.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried to do that at the beginning but it gave this error:illegal modifier for parameter StudentSpect;only final is permitted
I fixed this error too according to : stackoverflow.com/questions/29086903/… .it was really helpful thank you ❤❤❤
0

Array Index start with 0 and you have given size 2 means it can only have positions 0 and 1 not 2.

 public static String [][] StudentSpect = new String [n][2];
```
And you are accessing array position of 2 over here.
```
 StudentSpect[i][2] = SConverter.append(l).toString();
```
So make this Change

public static String [][] StudentSpect = new String [n][3];


4 Comments

You have inittialized StudentSpect before getting n variable initalied
Declare this on top public static String [][] StudentSpect;
initialize after n is initialized like this :- StudentSpect = new String [n][3];
Thanks for helping out 🙏

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.